Setup a default language
Presently, a project that uses linguist needs to provide all the translations for all the languages that it supports. It would be more convenient to fall back to a default language when a translation has not been made.
For example, we could declare the translations like this:
defmodule I18n do
use Linguist.Compiler,
default: :en,
locales: [
en: [
flash: [
notice: [
hello: "hello %{first} %{last}",
bye: "bye now, %{name}!"
]
],
users: [
title: "Users",
profiles: [
title: "Profiles",
]
]
],
fr: [
flash: [
notice: [
hello: "salut %{first} %{last}"
]
]
]
]
end
Then, if we call I18n.t("fr", "users.title"), it should return "Users". Optionally, we could log a message indicating that a translation is missing.
I'm against a default locale because it doesn't make sense, imo, to fallback to a different language when a given translation is unavailable. That said, here's my plan for the next release:
- Introduce a
t!function that raises if the translation is missing, and updatetto return{:ok, translation}or{:error :no_translation}. This would let the caller implement the interface that you'd like to see by doing:
def MyViewLayer do
@default_locale "en"
def t(locale, key, bindings \\ []) do
case I18n.t(locale, key, bindings) do
{:ok, translation} -> translation
{:error, :no_translation} -> I18n.t!(@default_locale, key, bindings)
end
end
end
Thoughts?
I think this is a good way to handle the "problem" described! It will be to the developer to choose what they want to do in case of a missing translation.