On-disk cache for downloaded locale data files.
This module is responsible for reading and writing locale ETF
files under the directory returned by
Localize.Locale.Provider.locale_cache_dir/0, and for detecting
when a cached file is stale relative to Localize.version/0.
Primary API
Summary
Types
A locale identifier as an atom.
Functions
Retrieves locale data from the cache directory.
Returns the absolute cache file path for the given locale.
Returns whether a cached locale file is stale.
Stores locale content in the cache directory.
Types
@type locale_id() :: atom()
A locale identifier as an atom.
Functions
@spec get(locale_id()) :: {:ok, map()} | {:error, Exception.t()}
Retrieves locale data from the cache directory.
Reads the locale's cache file, decodes it, and validates that
its :version field matches Localize.version/0.
Arguments
locale_idis a locale identifier atom.
Returns
{:ok, locale_data}if the cache file is present and its version matches the current version.{:error, Localize.LocaleIsStaleError.t()}if the cache file is present but its version does not match.{:error, Localize.LocaleNotFoundInCacheError.t()}if the cache file is not present.
Returns the absolute cache file path for the given locale.
Arguments
locale_idis a locale identifier atom.
Returns
- A string path to the locale's cache file.
Examples
iex> path = Localize.Locale.Provider.Cache.path(:en)
iex> String.ends_with?(path, "en.etf")
true
Returns whether a cached locale file is stale.
A cache file is considered stale if its :version field does
not equal Localize.version/0. A file that is missing or
unreadable is also considered stale.
Arguments
locale_idis a locale identifier atom.
Returns
trueif the cache file is missing, unreadable, or its version does not matchLocalize.version/0.falseif the cache file is present and its version matches.
@spec store(locale_id(), binary()) :: {:ok, String.t()} | {:error, Exception.t()}
Stores locale content in the cache directory.
Creates the cache directory if it does not already exist and
writes content to the locale's cache file. content should
be the raw binary form of the locale ETF file.
Arguments
locale_idis a locale identifier atom.contentis a binary containing the encoded locale data.
Returns
{:ok, path}wherepathis the absolute path the content was written to.{:error, Localize.LocaleCacheWriteError.t()}on failure.