Class: MyLanguageManager
- Inherits:
-
Object
- Object
- MyLanguageManager
- Defined in:
- app/my_lib/my_language_manager.rb
Overview
typed: true frozen_string_literal: true
Constant Summary collapse
- EN =
'en'- TH =
'th'- CN =
'cn'- ES =
'es'- FR =
'fr'- DE =
'de'- RU =
'ru'- MS =
'ms'- KO =
'ko'- JA =
'ja'- ID =
'id'- VI =
'vi'- ZH =
'zh'- DEFAULT_LANGUAGE =
EN- SUPPORTED_LANGUAGES =
All supported languages (matches MyLocaleManager::AVAILABLE_LOCALES) Order is important: longer/more specific codes should come before shorter ones ZH before CN to correctly match 'zh-CN' as 'zh' (Simplified) not 'cn' (Traditional)
[EN, TH, ZH, CN, ES, FR, DE, RU, MS, KO, JA, ID, VI].freeze
- LANGUAGE_PRIORITIES =
Priority order for language detection: TH first, ZH before CN, EN last
[TH, ZH, CN, ES, FR, DE, RU, MS, KO, JA, ID, VI, EN].freeze
Class Method Summary collapse
Class Method Details
.recognize(language) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/my_lib/my_language_manager.rb', line 28 def recognize(language) return DEFAULT_LANGUAGE if language.blank? normalized = language.to_s.downcase.strip # Check each language in priority order # Word boundaries avoid false matches in Accept-Language headers LANGUAGE_PRIORITIES.each do |lang| return lang if normalized =~ /\b#{lang}\b/i end DEFAULT_LANGUAGE end |
.validate(language) ⇒ Object
42 43 44 45 46 47 48 |
# File 'app/my_lib/my_language_manager.rb', line 42 def validate(language) return false if language.blank? # The "i" at the end of the regex means case-insensitive # Matches any of the supported language codes language.to_s =~ /^(en|th|cn|es|fr|de|ru|ms|ko|ja|id|vi|zh)/i end |