Localize.Locale.Loader (Localize v0.27.0)

Copy Markdown View Source

A GenServer that serializes locale data loading to prevent race conditions.

When multiple processes request the same locale concurrently, the first request triggers the load and subsequent requests wait for it to complete. This avoids duplicate loads and redundant :persistent_term.put/2 calls (each of which triggers a global GC).

The server is started as part of the Localize supervision tree. All locale loading goes through load_and_store/2 which delegates to this server.

Summary

Functions

Returns a specification to start this module under a supervisor.

Clears the locale validation cache.

Loads and stores locale data, serialized through the GenServer.

Starts the loader GenServer.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_locale_cache()

@spec clear_locale_cache() :: :ok

Clears the locale validation cache.

This is called when the supported locales change to ensure stale resolutions are not returned.

load_and_store(locale, options \\ [])

@spec load_and_store(Localize.Locale.Provider.locale(), Keyword.t()) ::
  :ok | {:error, Exception.t()}

Loads and stores locale data, serialized through the GenServer.

If the locale is already loaded, returns :ok immediately without contacting the server. Otherwise, delegates to the GenServer which ensures only one load per locale occurs.

Arguments

Options

Returns

  • :ok on success.

  • {:error, exception} if the locale data could not be loaded.

start_link(options \\ [])

Starts the loader GenServer.