Class: LocaleUtility
- Inherits:
-
Object
- Object
- LocaleUtility
- Extended by:
- Memery
- Defined in:
- app/lib/locale_utility.rb
Constant Summary collapse
- SITE_LOCALES =
%i[ en-US en-CA ]
- SITE_LOCALES_REGEX =
/#{SITE_LOCALES.join('|')}/- MARKET_LOCALES =
[ ['English', 'en'], ['English (US)', 'en-US'], ['English (CA)', 'en-CA'], ['French (CA)', 'fr-CA'] ].freeze
Class Method Summary collapse
-
.all_localized_fields(fields, locales = nil) ⇒ Array<String>
Returns a list of all localized field names for the given fields.
- .available_languages ⇒ Object
- .available_languages_for_select ⇒ Object
- .available_locales_for_select(locale_order = []) ⇒ Object
- .available_locales_for_select_without_default ⇒ Object
- .available_locales_sorted(locale_order = [:en]) ⇒ Object
-
.content_languages ⇒ Object
Defines the languages we will support for content editing.
- .content_languages_for_select ⇒ Object
-
.fastgettext_locale(locale = I18n.locale) ⇒ Object
Converts a i18n.locale to fastgettext format.
- .friendly_locale_name(locale = I18n.locale) ⇒ Object
- .iso3166_for_locale(locale = I18n.locale) ⇒ Object
- .language_name(lang) ⇒ Object
- .languages_for_country_iso(iso) ⇒ Object
- .languages_for_country_iso3(iso3) ⇒ Object
- .locale_for_iso3166_country(ic, include_fallbacks: false) ⇒ Object
- .locale_friendly_name(locale) ⇒ Object
- .locale_to_country_iso(locale = I18n.locale) ⇒ Object
- .locale_to_country_iso_short_name(locale = I18n.locale) ⇒ Object
- .locales_and_fallbacks(*locales) ⇒ Object
- .locales_for_country_iso(iso, include_fallbacks: false) ⇒ Object
- .locales_for_country_iso3(iso3, include_fallbacks: false) ⇒ Object
- .localizations_for_select(specific_locales = nil) ⇒ Object
-
.parameterized_field_name(field_name, locale = I18n.default_locale) ⇒ Object
Generates a parameterized field name suitable for Mobility.
-
.parameterized_locale(locale = I18n.default_locale) ⇒ Object
Parameterize the locale for a form field with Mobility.
-
.root_content_locale(locale = nil) ⇒ Object
Find the root language fallback.
-
.service_locales ⇒ Object
Defines the locales that our web site can serve via url, e.g /en-US, /en-CA When adding new locales, please update also - primeval.js, const validLocales - Cloudflare cache eligibility rules: https://dash.cloudflare.com/79b7f58cf035093b5ad11747df30369a/warmlyyours.com/caching/cache-rules/new.
Class Method Details
.all_localized_fields(fields, locales = nil) ⇒ Array<String>
Returns a list of all localized field names for the given fields.
163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/lib/locale_utility.rb', line 163 def all_localized_fields(fields, locales = nil) localized_field_names = [] fields.each do |attr| LocaleUtility.available_locales_sorted.each do |locale| next if locales.present? && locales.include?(locale) localized_field_names << LocaleUtility.parameterized_field_name(attr, locale) end end localized_field_names end |
.available_languages ⇒ Object
142 143 144 |
# File 'app/lib/locale_utility.rb', line 142 def available_languages LanguageList::COMMON_LANGUAGES.map(&:name) end |
.available_languages_for_select ⇒ Object
138 139 140 |
# File 'app/lib/locale_utility.rb', line 138 def available_languages_for_select LanguageList::COMMON_LANGUAGES.map { |l| [l.name, l.iso_639_1] } end |
.available_locales_for_select(locale_order = []) ⇒ Object
118 119 120 |
# File 'app/lib/locale_utility.rb', line 118 def available_locales_for_select(locale_order = []) available_locales_sorted(locale_order).map { |locale| [friendly_locale_name(locale), locale] } end |
.available_locales_for_select_without_default ⇒ Object
122 123 124 |
# File 'app/lib/locale_utility.rb', line 122 def available_locales_for_select_without_default available_locales_for_select[1..] end |
.available_locales_sorted(locale_order = [:en]) ⇒ Object
110 111 112 113 114 115 116 |
# File 'app/lib/locale_utility.rb', line 110 def available_locales_sorted(locale_order = [:en]) # For now we will just take whatever the natural order of I18n available locale is available_locales = Mobility.available_locales available_locales.sort_by do |locale| locale_order.index(locale) || Float::INFINITY end end |
.content_languages ⇒ Object
Defines the languages we will support for content editing
27 28 29 |
# File 'app/lib/locale_utility.rb', line 27 def content_languages I18n.available_locales.map { |l| l.to_s.first(2).to_sym }.uniq.sort end |
.content_languages_for_select ⇒ Object
31 32 33 |
# File 'app/lib/locale_utility.rb', line 31 def content_languages_for_select content_languages.map { |lang| [LanguageList::LanguageInfo.find(lang.to_s).name, lang] } end |
.fastgettext_locale(locale = I18n.locale) ⇒ Object
Converts a i18n.locale to fastgettext format
22 23 24 |
# File 'app/lib/locale_utility.rb', line 22 def fastgettext_locale(locale = I18n.locale) locale.to_s.tr('-', '_') end |
.friendly_locale_name(locale = I18n.locale) ⇒ Object
126 127 128 129 130 131 132 |
# File 'app/lib/locale_utility.rb', line 126 def friendly_locale_name(locale = I18n.locale) lang = locale.to_s.first(2) country = locale_to_country_iso_short_name(locale) fln = language_name(lang) fln << " (#{country})" if country fln end |
.iso3166_for_locale(locale = I18n.locale) ⇒ Object
76 77 78 79 80 81 |
# File 'app/lib/locale_utility.rb', line 76 def iso3166_for_locale(locale = I18n.locale) _, country_iso = locale.to_s.split('-') return unless country_iso ISO3166::Country[country_iso] end |
.language_name(lang) ⇒ Object
134 135 136 |
# File 'app/lib/locale_utility.rb', line 134 def language_name(lang) LanguageList::LanguageInfo.find(lang.first(2)).name.dup end |
.languages_for_country_iso(iso) ⇒ Object
93 94 95 96 |
# File 'app/lib/locale_utility.rb', line 93 def languages_for_country_iso(iso) ic = ISO3166::Country.find_country_by_alpha2(iso) ic.languages_spoken end |
.languages_for_country_iso3(iso3) ⇒ Object
98 99 100 101 |
# File 'app/lib/locale_utility.rb', line 98 def languages_for_country_iso3(iso3) ic = ISO3166::Country.find_country_by_alpha3(iso3) ic.languages_spoken end |
.locale_for_iso3166_country(ic, include_fallbacks: false) ⇒ Object
103 104 105 106 107 108 |
# File 'app/lib/locale_utility.rb', line 103 def locale_for_iso3166_country(ic, include_fallbacks: false) locales = ic.languages_spoken.map { |l| :"#{l.downcase}-#{ic.alpha2.upcase}" } # Get fallbacks locales += locales.map { |l| root_content_locale(l) } if include_fallbacks locales.uniq end |
.locale_friendly_name(locale) ⇒ Object
41 42 43 44 45 46 |
# File 'app/lib/locale_utility.rb', line 41 def locale_friendly_name(locale) language_code, country_code = locale.to_s.split('-') option_name = "#{LanguageList::LanguageInfo.find(language_code.to_s).name}" option_name << " (#{country_code})" if country_code.present? option_name end |
.locale_to_country_iso(locale = I18n.locale) ⇒ Object
83 84 85 |
# File 'app/lib/locale_utility.rb', line 83 def locale_to_country_iso(locale = I18n.locale) locale.to_s.split('-').last end |
.locale_to_country_iso_short_name(locale = I18n.locale) ⇒ Object
87 88 89 90 91 |
# File 'app/lib/locale_utility.rb', line 87 def locale_to_country_iso_short_name(locale = I18n.locale) return unless ic = iso3166_for_locale(locale) ic.alpha3 == 'USA' ? 'USA' : ic.iso_short_name end |
.locales_and_fallbacks(*locales) ⇒ Object
175 176 177 |
# File 'app/lib/locale_utility.rb', line 175 def locales_and_fallbacks(*locales) [locales].flatten.flat_map { |l| [l, root_content_locale(l)] }.uniq.map(&:presence).compact end |
.locales_for_country_iso(iso, include_fallbacks: false) ⇒ Object
71 72 73 74 |
# File 'app/lib/locale_utility.rb', line 71 def locales_for_country_iso(iso, include_fallbacks: false) ic = ISO3166::Country.find_country_by_alpha2(iso) locale_for_iso3166_country(ic, include_fallbacks:) end |
.locales_for_country_iso3(iso3, include_fallbacks: false) ⇒ Object
66 67 68 69 |
# File 'app/lib/locale_utility.rb', line 66 def locales_for_country_iso3(iso3, include_fallbacks: false) ic = ISO3166::Country.find_country_by_alpha3(iso3) locale_for_iso3166_country(ic, include_fallbacks:) end |
.localizations_for_select(specific_locales = nil) ⇒ Object
35 36 37 38 39 |
# File 'app/lib/locale_utility.rb', line 35 def localizations_for_select(specific_locales = nil) locales = I18n.available_locales locales = locales.select { |l| l.to_s.in?(specific_locales.map(&:to_s)) } if specific_locales.present? locales.map { |locale| [locale_friendly_name(locale), locale] } end |
.parameterized_field_name(field_name, locale = I18n.default_locale) ⇒ Object
Generates a parameterized field name suitable for Mobility
152 153 154 155 156 |
# File 'app/lib/locale_utility.rb', line 152 def parameterized_field_name(field_name, locale = I18n.default_locale) s = "#{field_name}" # ensures symbols are string and strings are duplicate objects s << "_#{parameterized_locale(locale)}" unless locale.to_sym == I18n.default_locale s end |
.parameterized_locale(locale = I18n.default_locale) ⇒ Object
Parameterize the locale for a form field with Mobility
147 148 149 |
# File 'app/lib/locale_utility.rb', line 147 def parameterized_locale(locale = I18n.default_locale) locale.to_s.downcase.tr('-', '_') end |
.root_content_locale(locale = nil) ⇒ Object
Find the root language fallback
60 61 62 63 64 |
# File 'app/lib/locale_utility.rb', line 60 def root_content_locale(locale = nil) return nil if locale.nil? I18n.fallbacks[locale]&.last end |
.service_locales ⇒ Object
Defines the locales that our web site can serve via url, e.g /en-US, /en-CA
When adding new locales, please update also
- primeval.js, const validLocales
- Cloudflare cache eligibility rules: https://dash.cloudflare.com/79b7f58cf035093b5ad11747df30369a/warmlyyours.com/caching/cache-rules/new
52 53 54 55 56 57 |
# File 'app/lib/locale_utility.rb', line 52 def service_locales # What your app localizes for and what our web site respond to are different things, hardcoded supported locales now but # for reference this is commented out: # (I18n.available_locales - [:en, :es]).freeze SITE_LOCALES end |