PhoenixKit.Modules.Publishing.Web.Controller.Language (phoenix_kit v1.7.71)

Copy Markdown View Source

Language detection and resolution for the publishing controller.

Handles detecting whether URL parameters represent language codes, resolving language codes to file languages, and determining canonical URL language codes.

Summary

Functions

Checks if a code is a base (2-letter) language code.

Detects if the :group route param is actually a language code by checking if content exists.

Detects whether the 'language' parameter is actually a language code or a group slug.

Find a dialect in enabled languages that matches the given base code.

Find a dialect in available languages that matches the given base code.

Gets the canonical URL language code for a given language. If multiple dialects of the same base language are enabled, returns the full dialect. Otherwise returns the base code for cleaner URLs.

Gets the canonical URL language code for a post's language. This uses the actual file language (e.g., "en-US") to determine the canonical URL code.

Gets the default language.

Gets the list of enabled language codes.

Gets a language's flag emoji.

Gets a language's display name.

Check if any post in the group has content for the given language. Uses listing cache when available for fast lookups.

Checks if a string looks like a language code pattern. Matches: 2-letter codes (en, fr), or dialect codes (en-US, pt-BR)

Resolves a language code to an actual content language. Handles base codes by finding a matching dialect in available languages.

Validates if a code represents a valid language.

Functions

base_code?(code)

Checks if a code is a base (2-letter) language code.

detect_language_in_group_param(params)

Detects if the :group route param is actually a language code by checking if content exists.

Returns {:language_detected, language, adjusted_params} or :not_a_language

detect_language_or_group(language_param, params)

Detects whether the 'language' parameter is actually a language code or a group slug.

This allows the same route pattern (/:language/:group/*path) to work for both:

  • Multi-language: /en/my-group/my-post (language=en, group=my-group)
  • Single-language: /my-group/my-post (language=my-group, needs adjustment)

Returns {detected_language, adjusted_params}

find_dialect_for_base(base_code, enabled_languages)

Find a dialect in enabled languages that matches the given base code.

find_dialect_for_base_in_languages(base_code, available_languages)

Find a dialect in available languages that matches the given base code.

get_canonical_url_language(language)

Gets the canonical URL language code for a given language. If multiple dialects of the same base language are enabled, returns the full dialect. Otherwise returns the base code for cleaner URLs.

get_canonical_url_language_for_post(post_language)

Gets the canonical URL language code for a post's language. This uses the actual file language (e.g., "en-US") to determine the canonical URL code.

get_default_language()

Gets the default language.

get_enabled_languages()

Gets the list of enabled language codes.

get_language_flag(code)

Gets a language's flag emoji.

get_language_name(code)

Gets a language's display name.

has_content_for_language?(group_slug, language)

Check if any post in the group has content for the given language. Uses listing cache when available for fast lookups.

looks_like_language_code?(code)

Checks if a string looks like a language code pattern. Matches: 2-letter codes (en, fr), or dialect codes (en-US, pt-BR)

resolve_language_for_post(language, available_languages)

Resolves a language code to an actual content language. Handles base codes by finding a matching dialect in available languages.

valid_language?(code)

Validates if a code represents a valid language.