linguist icon indicating copy to clipboard operation
linguist copied to clipboard

Setup a default language

Open magicienap opened this issue 11 years ago • 2 comments

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.

magicienap avatar Jun 29 '14 12:06 magicienap

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:

  1. Introduce a t! function that raises if the translation is missing, and update t to 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?

chrismccord avatar Jun 29 '14 17:06 chrismccord

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.

magicienap avatar Jun 30 '14 22:06 magicienap