Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cschmatzler committed Jul 25, 2023
1 parent 7308472 commit 644bac3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Depending on which source you decide to add, you might also need to configure it

## Configuration

The default locale and fallback can be set in `config.exs`:
The default locale, fallback and namespace can be set in `config.exs`:

```
config :idiom,
default_locale: "fr",
default_fallback: "en"
default_fallback: "en",
default_namespace: "translations"
```

These defaults will be used when the option isn't passed to either `t` itself or set in the process dictionary.
Expand Down Expand Up @@ -96,12 +97,21 @@ should offer the following suffixes for translations that support pluralization:

You can then pass a `count` to `t`. `count` can be an integer, string, float or `Decimal`.

## Interpolation
### Interpolation

Idiom also supports interpolation in your translations. Variables can be marked inside `{{}}`, for example `Hello, {{name}}`.
You can then pass bindings to `t` as second parameter, such as `t("Hello, {{name}}!", %{name: "world"}, to: "de")`.
If the variable has no binding, it will be left as-is, without the braces: `t("Hello, {{name}}!", %{}, to: "en")` results in `Hello, name!`.

### Namespaces

Keys can be separated into different namespaces. These can be accessed in multiple ways (in order of priority):

- In the key itself, as a prefix separated by a colon: `t("signup:Create account")`
- As an option: `t("Create account", namespace: "signup")`
- As a key in the process dictionary: `Process.put(:namespace, "signup")`
- The default namespace set in `config.exs` (see [Configuration](#configuration))

## Sources

### Local
Expand Down
6 changes: 3 additions & 3 deletions lib/idiom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ defmodule Idiom do

@doc false
defp extract_namespace(key, opts) do
default_namespace = Keyword.get(opts, :default_namespace) || Application.get_env(:idiom, :default_namespace)
namespace_from_opts = Keyword.get(opts, :namespace) || Process.get(:namespace) || Application.get_env(:idiom, :default_namespace)
namespace_separator = Keyword.get(opts, :namespace_separator, ":")
key_separator = Keyword.get(opts, :key_separator, ".")

if String.contains?(key, namespace_separator) do
[namespace | key_parts] = String.split(key, namespace_separator)

if is_binary(Keyword.get(opts, :namespace)) or is_binary(Process.get(:idiom_namespace)) do
if is_binary(Keyword.get(opts, :namespace)) or is_binary(Process.get(:namespace)) do
Logger.warning("Namespace was set in options/process, but key #{key} already includes a namespace. Using the key's namespace: #{namespace}.")
end

{namespace, Enum.join(key_parts, key_separator)}
else
{default_namespace, key}
{namespace_from_opts, key}
end
end
end

0 comments on commit 644bac3

Please sign in to comment.