PhoenixKit. Modules. Publishing. LanguageHelpers
(PhoenixKitPublishing v0.1.10)
Copy Markdown
View Source
Pure language utility functions for the Publishing module.
Provides language detection, display ordering, language info lookup, and primary language management.
Summary
Functions
Returns true for bare base language codes like "en" or "de".
Builds a single language entry map for a post.
Builds language data for a post's language switcher. Returns a list of language maps with status, enabled flag, known flag, and metadata.
Returns true when public URLs should omit the prefix for the default language.
Returns all enabled language codes for multi-language support. Falls back to content language if Languages module is disabled.
Determines the display code for a language based on whether multiple dialects of the same base language are enabled.
Gets language details (name, flag) for a given language code.
Returns the primary/canonical language for versioning. Uses Settings.get_content_language().
Returns the primary language code as it should appear in public URLs.
Checks if a language code is enabled, considering base code matching.
Removes base-only language codes when a dialect of the same base is also enabled.
Orders languages for display in the language switcher.
Checks if a language code is reserved (cannot be used as a slug).
Finds a language in candidates whose dialect base matches base_code.
Resolves a display language code to a key in a language map.
Returns true when the site should behave as single-language for public URLs.
Normalizes a content language code for use in public URLs.
Returns true when public URLs should include a language prefix.
Functions
Returns true for bare base language codes like "en" or "de".
Builds a single language entry map for a post.
Builds language data for a post's language switcher. Returns a list of language maps with status, enabled flag, known flag, and metadata.
@spec default_language_no_prefix?() :: boolean()
Returns true when public URLs should omit the prefix for the default language.
@spec enabled_language_codes() :: [String.t()]
Returns all enabled language codes for multi-language support. Falls back to content language if Languages module is disabled.
Determines the display code for a language based on whether multiple dialects of the same base language are enabled.
If only one dialect of a base language is enabled (e.g., just "en-US"), returns the base code ("en") for cleaner display.
If multiple dialects are enabled (e.g., "en-US" and "en-GB"), returns the full dialect code ("en-US") to distinguish them.
@spec get_language_info(String.t()) :: %{code: String.t(), name: String.t(), flag: String.t()} | nil
Gets language details (name, flag) for a given language code.
Searches in order:
- Predefined languages (BeamLabCountries) - for full locale details
- User-configured languages - for custom/less common languages
@spec get_primary_language() :: String.t()
Returns the primary/canonical language for versioning. Uses Settings.get_content_language().
@spec get_primary_language_base() :: String.t()
Returns the primary language code as it should appear in public URLs.
Content rows keep using the full configured language code (for example
"en-GB"), but public routes use the base code ("en").
Checks if a language code is enabled, considering base code matching.
Handles cases where:
- The code is
enand enabled languages has"en-US"-> matches - The code is
en-USand enabled languages has"en"-> matches
Removes base-only language codes when a dialect of the same base is also enabled.
Publishing stores and routes against dialects when they exist. If both "en"
and "en-US" are enabled in the broader Languages config, Publishing should
treat the bare base code as legacy compatibility data, not as a separate
translation target.
Orders languages for display in the language switcher.
Order: primary language first, then languages with translations (sorted), then languages without translations (sorted).
Checks if a language code is reserved (cannot be used as a slug).
Finds a language in candidates whose dialect base matches base_code.
Consolidates three near-identical helpers that used to live in
Posts, Web.Controller.Language, and StaleFixer — each
reimplementing the same "find a dialect for this base code" logic
with slightly different tie-breaks and exclusion rules.
Options
:prefer— when multiple candidates match the base, prefer this specific code if it's among the matches. Useful for primary-language preference; passLanguageHelpers.get_primary_language()to get the historical "primary wins" tie-break. Note: this option is explicit (not an implicit DB call inside the function) so callers decide when to incur the cached lookup.:exclude— code or list of codes to drop from the candidate set before matching. Used by stale-language self-healing to require a TRUE dialect (e.g. exclude"en"when searching for base"en"so the result is always something like"en-US").
Returns the chosen language code, or nil when no candidate matches.
Examples
iex> LanguageHelpers.resolve_dialect_for_base("en", ["en-US", "fr-FR"])
"en-US"
iex> LanguageHelpers.resolve_dialect_for_base("en", ["en", "en-US"], prefer: "en-US")
"en-US"
iex> LanguageHelpers.resolve_dialect_for_base("en", ["en", "en-US"], exclude: "en")
"en-US"
iex> LanguageHelpers.resolve_dialect_for_base("xx", ["en", "fr"])
nil
Resolves a display language code to a key in a language map.
Language maps (e.g., language_titles, language_slugs) use full dialect
codes as keys (e.g., "en-US"), but the display/canonical language may be
a base code (e.g., "en") when only one dialect is enabled.
Tries exact match first, then falls back to base code matching.
@spec single_language_mode?() :: boolean()
Returns true when the site should behave as single-language for public URLs.
Normalizes a content language code for use in public URLs.
Returns true when public URLs should include a language prefix.
Prefixes are omitted when:
- the site is effectively single-language, or
- the caller requested the default language and the
publishing_default_language_no_prefixsetting is enabled.