Module: ActiveLrs::Xapi::LocalizationHelper
- Included in:
- Statement, ActivityDefinition, Attachment, InteractionComponent, Verb
- Defined in:
- lib/active_lrs/xapi/localization_helper.rb
Overview
Helper methods for resolving localized strings from xAPI statements.
Provides functionality to select the most appropriate value from a language map, following xAPI spec conventions and fallbacks.
Instance Method Summary collapse
-
#get_localized_value(lang_map, locale = nil) ⇒ String
Resolve the most appropriate localized string from a language map (per xAPI spec), following a series of fallbacks.
Instance Method Details
#get_localized_value(lang_map, locale = nil) ⇒ String
Resolve the most appropriate localized string from a language map (per xAPI spec), following a series of fallbacks.
The resolution order is:
1. Exact match for the given locale (e.g., "en-US")
2. Variant match of the requested locale (e.g., "en" → "en-US", "en-CA")
3. Exact match for the system default locale (ActiveLrs.configuration.default_locale)
4. Variant match for the system default locale (e.g., "fr" → "fr-CA")
5. First available value in the map
If no match is found, returns the string ‘“undefined”`.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_lrs/xapi/localization_helper.rb', line 32 def get_localized_value(lang_map, locale = nil) return nil unless lang_map.is_a?(Hash) && lang_map.any? # Determine locale in safe order locale ||= ActiveLrs.configuration.default_locale || (defined?(I18n) && I18n.locale) locale = locale.to_s # Exact match return lang_map[locale] if lang_map.key?(locale) # Match variant for requested locale (e.g., "en" → "en-US") variant_value = find_variant(lang_map, locale) return variant_value if variant_value # Fallback to system default locale default_locale = ActiveLrs.configuration.default_locale.to_s if default_locale != locale return lang_map[default_locale] if lang_map.key?(default_locale) # Match variant for default locale (e.g., "fr" → "fr-CA") variant_value = find_variant(lang_map, default_locale) return variant_value if variant_value end # Fallback to first available value lang_map.values.first || nil end |