ElementorexCms.I18n (ElementorexCms v0.2.0)

Copy Markdown View Source

Translations of the editor interface and the built-in blocks.

Every user-facing string of the library is written in English and looked up in the dictionary of the current locale when it is rendered. English is the default; the library ships Spanish ("es"), Portuguese ("pt"), German ("de"), French ("fr"), Italian ("it") and Japanese ("ja").

The locale is per process, like Gettext's: the editor sets it from its locale attribute on every update, so the LiveView that hosts it renders in that language. Pages pass locale to ElementorexCms.Components.document/1. Elsewhere use put_locale/1, with_locale/2 or configure a default:

config :elementorex_cms, locale: "es"

Regional codes fall back to their language: "pt-BR" and "fr_CA" use the "pt" and "fr" dictionaries.

Your own strings and languages

The labels of your blocks and templates are translated like the built-in ones. Put them in locale modules (see ElementorexCms.I18n.Locale), which mix elementorex.gen.locale creates with every string to translate:

$ mix elementorex.gen.locale es pt          # your blocks in two languages
$ mix elementorex.gen.locale ko --name 한국어  # a new language, everything

config :elementorex_cms,
  locales: [MyAppWeb.ElementorexLocales.Es, MyAppWeb.ElementorexLocales.Pt]

A few strings can also go straight in the config; they win over everything:

config :elementorex_cms, :translations, %{"es" => %{"Pricing" => "Precios"}}

Lookup order: :translations → the app's locale modules → the built-in dictionary → the English text.

Summary

Functions

The built-in locale modules (see ElementorexCms.I18n.Locale).

The effective dictionary of a locale: the built-in one, the app's locale modules and the :translations config, merged in that order (nil values are left out).

{locale, name} of every available locale, with the name of each language in that language: for language switchers.

Locale of the current process, falling back to the configured default.

Available locales: English, the built-in ones and the new languages of the configured locale modules.

Sets the locale of the current process ("en", "es", "pt-BR"...).

The English strings of the library that are translated: the keys of every built-in dictionary.

The translatable strings of block modules (ElementorexCms.Block), template modules (ElementorexCms.Templates) and sources of dynamic content (ElementorexCms.Source, as listed in the :sources config): labels, descriptions, help texts, groups, option names, the custom category, default texts and the names of sources and their fields. Used by mix elementorex.gen.locale.

Translates text to the current locale. bindings replace %{name} placeholders after the lookup.

Runs fun with locale as the locale of the process and restores the previous one afterwards. nil keeps the current locale.

Functions

builtin()

@spec builtin() :: [module()]

The built-in locale modules (see ElementorexCms.I18n.Locale).

dictionary(locale)

@spec dictionary(String.t()) :: %{required(String.t()) => String.t()}

The effective dictionary of a locale: the built-in one, the app's locale modules and the :translations config, merged in that order (nil values are left out).

languages()

@spec languages() :: [{String.t(), String.t()}]

{locale, name} of every available locale, with the name of each language in that language: for language switchers.

locale()

@spec locale() :: String.t()

Locale of the current process, falling back to the configured default.

locales()

@spec locales() :: [String.t()]

Available locales: English, the built-in ones and the new languages of the configured locale modules.

iex> ElementorexCms.I18n.locales()
["en", "es", "pt", "de", "fr", "it", "ja"]

put_locale(locale)

@spec put_locale(String.t() | atom() | nil) :: String.t() | nil

Sets the locale of the current process ("en", "es", "pt-BR"...).

source_strings()

@spec source_strings() :: [String.t()]

The English strings of the library that are translated: the keys of every built-in dictionary.

strings(modules)

@spec strings([module() | {module(), keyword()}]) :: [String.t()]

The translatable strings of block modules (ElementorexCms.Block), template modules (ElementorexCms.Templates) and sources of dynamic content (ElementorexCms.Source, as listed in the :sources config): labels, descriptions, help texts, groups, option names, the custom category, default texts and the names of sources and their fields. Used by mix elementorex.gen.locale.

t(text, bindings \\ [])

@spec t(String.t() | nil, keyword() | map()) :: String.t()

Translates text to the current locale. bindings replace %{name} placeholders after the lookup.

iex> ElementorexCms.I18n.put_locale("es")
iex> ElementorexCms.I18n.t("Button")
"Botón"
iex> ElementorexCms.I18n.t("%{count} items", count: 3)
"3 elementos"

with_locale(locale, fun)

@spec with_locale(String.t() | atom() | nil, (-> result)) :: result when result: var

Runs fun with locale as the locale of the process and restores the previous one afterwards. nil keeps the current locale.

iex> ElementorexCms.I18n.with_locale("de", fn -> ElementorexCms.I18n.t("Button") end)
"Button"
iex> ElementorexCms.I18n.with_locale("ja", fn -> ElementorexCms.I18n.t("Save") end)
"保存"