PhoenixKit.Modules.Publishing.LanguageHelpers
(PhoenixKitPublishing v0.1.6)
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).
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).
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.