PureAdmin.Translations (PureAdmin v1.1.0)

Copy Markdown View Source

Translation system for PureAdmin components.

Ships with English defaults that work out of the box. Applications can override translations at runtime by providing a callback function via config:

# config/config.exs
config :keen_pure_admin,
  translate: &MyApp.Translations.translate/2

The callback receives a translation key and a params map:

defmodule MyApp.Translations do
  def translate(key, params) do
    # Load from DB, Gettext, ETS, etc.
    translation = MyApp.Repo.get_translation(key, current_locale())
    # Use the interpolation helper to replace %{param} placeholders
    PureAdmin.Translations.interpolate(translation, params)
  end
end

If the callback returns nil, the English default is used as fallback.

Translation Keys

All keys follow the flat pureAdmin.* convention:

  • pureAdmin.buttons.* — Cancel, Confirm, Close, OK
  • pureAdmin.search.* — Searching..., No results
  • pureAdmin.pagination.* — First/Prev/Next/Last Page, Load More, page counts
  • pureAdmin.commandPalette.* — Placeholder, hints, step indicators
  • pureAdmin.popconfirm.* — Confirm, Cancel
  • pureAdmin.settings.* — Theme, Mode, Layout Width, etc.
  • pureAdmin.a11y.* — Accessibility labels

Interpolation

Use %{param} placeholders in translation strings:

t("pureAdmin.pagination.pages", %{page: 2, total: 5})
# => "2 of 5 pages"

t("pureAdmin.commandPalette.stepOf", %{current: 1, total: 3})
# => "Step 1 of 3"

Summary

Functions

Returns the default English translation for a key with interpolation.

Returns the full map of default English translations.

Interpolates %{param} placeholders in a string with values from the params map.

Translates a key with optional parameter interpolation.

Functions

default(key, params \\ %{})

@spec default(String.t(), map()) :: String.t()

Returns the default English translation for a key with interpolation.

Returns the key itself if no default exists.

defaults()

@spec defaults() :: map()

Returns the full map of default English translations.

Useful for apps that want to see all available keys.

interpolate(string, params)

@spec interpolate(String.t(), map()) :: String.t()

Interpolates %{param} placeholders in a string with values from the params map.

Exported for use by app translation callbacks.

Examples

interpolate("Found %{count} results", %{count: 3})
# => "Found 3 results"

interpolate("Page %{page} of %{total}", %{page: 2, total: 5})
# => "Page 2 of 5"

t(key, params \\ %{})

@spec t(String.t(), map()) :: String.t()

Translates a key with optional parameter interpolation.

Calls the app-configured callback if set, falls back to English defaults.

Examples

t("pureAdmin.buttons.cancel")
# => "Cancel"

t("pureAdmin.pagination.pages", %{total: 5})
# => "/ 5 pages"

t("pureAdmin.commandPalette.stepOf", %{current: 1, total: 3})
# => "Step 1 of 3"