Idiom.Cache (idiom v0.6.8)

Cache for translations.

Wraps an ETS table and offers functions to insert and fetch localisation data. table

Summary

Functions

Link to this function

get_translation(locale, namespace, key, table_name \\ :idiom_cache)

@spec get_translation(String.t(), String.t(), String.t(), atom()) :: String.t() | nil

Retrieves a translation from the cache.

Examples

iex> Cache.get_translation("de", "default", "butterfly")
"Schmetterling"
Link to this function

init(initial_state \\ %{}, table_name \\ :idiom_cache)

@spec init(map(), atom()) :: :ok

Starts a new cache.

Allows adding initial state by passing it as first parameter.

Parameters

  • initial_state - State to initialise the cache with. See the documentation for insert_keys/2 for the expected format.

Examples

iex> initial_state = %{
...>  "en" => %{"signup" => %{"Create your account" => "Create your account"}}, 
...>  "de" => %{"signup" => %{"Create your account" => "Erstelle deinen Account"}}
...>}
iex> Idiom.Cache.init(initial_state)
:ok

iex> Idiom.Cache.init(%{}, :different_cache_name)
:ok
Link to this function

insert_keys(keys, table_name \\ :idiom_cache)

@spec insert_keys(map(), atom()) :: :ok

Adds a map of keys to the cache.

Parameters

  • keys - Map of keys to add to the cache.

Format of keys

%{
  "en" => %{"signup" => %{"Create your account" => "Create your account"}}, 
  "de" => %{"signup" => %{"Create your account" => "Erstelle deinen Account"}}}
}

where the first level is the locale, the second the namespace, and the third a map of the keys contained in the previous two. The keys can be nested further, the cache will automatically flatten them as such:

%{
  "en" => %{
    "signup" => %{
      "multiple" => %{
        "levels" => %{
          "nesting" => "Hello!"
        }
      }
    }
  }
}

will result in a key of multiple.levels.nesting inside the signup namespace with a message value of Hello!.

Examples

iex> Idiom.Cache.insert_keys(%{
...>  "en" => %{"signup" => %{"Create your account" => "Create your account"}}, 
...>  "de" => %{"signup" => %{"Create your account" => "Erstelle deinen Account"}}}
...>)
:ok