Passing a custom variable to i18n

Yes, you can pass any variable to the message.

// i18n/en.toml
[results]
one = "Found 1 result in {{ .Time }}"
other = "Found {{ .Count }} results in {{ .Time }}"

[echo]
other = "echo {{ . }}"
{{- warnf "%v" (i18n "results" (dict "Count" 1 "Time" "10ms")) }}
{{- warnf "%v" (i18n "results" (dict "Count" 20 "Time" "0.2s")) }}
{{- warnf "%v" (i18n "echo" "Hi there!") }}
WARN 2023/04/20 21:41:22 Found 1 result in 10ms
WARN 2023/04/20 21:41:22 Found 20 results in 0.2s
WARN 2023/04/20 21:41:22 echo Hi there!

Explanation:

  • If I remember correctly, the Count is used to determine the singular (one) and plural (other).
  • You can access the data via context (AKA the dot .).
  • The Count isn’t required, you can pass data in any type, such as a string.
2 Likes