Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup a default language #10

Open
magicienap opened this issue Jun 29, 2014 · 2 comments
Open

Setup a default language #10

magicienap opened this issue Jun 29, 2014 · 2 comments

Comments

@magicienap
Copy link
Contributor

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.

@chrismccord
Copy link
Contributor

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?

@magicienap
Copy link
Contributor Author

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.

mertonium pushed a commit that referenced this issue Apr 2, 2020
CHANGE-47963 - add sobelow and review the vulnerabilities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants