Class: LocaleUtility
- Inherits:
-
Object
- Object
- LocaleUtility
- Extended by:
- Memery
- Defined in:
- app/lib/locale_utility.rb
Overview
Library code: locale utility.
Constant Summary collapse
- SITE_LOCALES =
Site locales.
%i[ en-US en-CA ].freeze
- SITE_LOCALES_REGEX =
Regex pattern matching site locales.
/#{SITE_LOCALES.join('|')}/- MARKET_LOCALES =
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.
167 168 169 170 171 172 173 174 175 176 177 |
# File 'app/lib/locale_utility.rb', line 167 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
147 148 149 |
# File 'app/lib/locale_utility.rb', line 147 def available_languages LanguageList::COMMON_LANGUAGES.map(&:name) end |
.available_languages_for_select ⇒ Object
143 144 145 |
# File 'app/lib/locale_utility.rb', line 143 def available_languages_for_select LanguageList::COMMON_LANGUAGES.map { |l| [l.name, l.iso_639_1] } end |
.available_locales_for_select(locale_order = []) ⇒ Object
123 124 125 |
# File 'app/lib/locale_utility.rb', line 123 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
127 128 129 |
# File 'app/lib/locale_utility.rb', line 127 def available_locales_for_select_without_default available_locales_for_select[1..] end |
.available_locales_sorted(locale_order = [:en]) ⇒ Object
115 116 117 118 119 120 121 |
# File 'app/lib/locale_utility.rb', line 115 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
32 33 34 |
# File 'app/lib/locale_utility.rb', line 32 def content_languages I18n.available_locales.map { |l| l.to_s.first(2).to_sym }.uniq.sort end |
.content_languages_for_select ⇒ Object
36 37 38 |
# File 'app/lib/locale_utility.rb', line 36 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
27 28 29 |
# File 'app/lib/locale_utility.rb', line 27 def fastgettext_locale(locale = I18n.locale) locale.to_s.tr('-', '_') end |
.friendly_locale_name(locale = I18n.locale) ⇒ Object
131 132 133 134 135 136 137 |
# File 'app/lib/locale_utility.rb', line 131 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
81 82 83 84 85 86 |
# File 'app/lib/locale_utility.rb', line 81 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
139 140 141 |
# File 'app/lib/locale_utility.rb', line 139 def language_name(lang) LanguageList::LanguageInfo.find(lang.first(2)).name.dup end |
.languages_for_country_iso(iso) ⇒ Object
98 99 100 101 |
# File 'app/lib/locale_utility.rb', line 98 def languages_for_country_iso(iso) ic = ISO3166::Country.find_country_by_alpha2(iso) ic.languages_spoken end |
.languages_for_country_iso3(iso3) ⇒ Object
103 104 105 106 |
# File 'app/lib/locale_utility.rb', line 103 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
108 109 110 111 112 113 |
# File 'app/lib/locale_utility.rb', line 108 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
46 47 48 49 50 51 |
# File 'app/lib/locale_utility.rb', line 46 def locale_friendly_name(locale) language_code, country_code = locale.to_s.split('-') option_name = LanguageList::LanguageInfo.find(language_code.to_s).name.to_s option_name += " (#{country_code})" if country_code.present? option_name end |
.locale_to_country_iso(locale = I18n.locale) ⇒ Object
88 89 90 |
# File 'app/lib/locale_utility.rb', line 88 def locale_to_country_iso(locale = I18n.locale) locale.to_s.split('-').last end |
.locale_to_country_iso_short_name(locale = I18n.locale) ⇒ Object
92 93 94 95 96 |
# File 'app/lib/locale_utility.rb', line 92 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
179 180 181 |
# File 'app/lib/locale_utility.rb', line 179 def locales_and_fallbacks(*locales) [locales].flatten.flat_map { |l| [l, root_content_locale(l)] }.uniq.filter_map(&:presence) end |
.locales_for_country_iso(iso, include_fallbacks: false) ⇒ Object
76 77 78 79 |
# File 'app/lib/locale_utility.rb', line 76 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
71 72 73 74 |
# File 'app/lib/locale_utility.rb', line 71 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
40 41 42 43 44 |
# File 'app/lib/locale_utility.rb', line 40 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
157 158 159 160 |
# File 'app/lib/locale_utility.rb', line 157 def parameterized_field_name(field_name, locale = I18n.default_locale) base = field_name.to_s locale.to_sym == I18n.default_locale ? base : "#{base}_#{parameterized_locale(locale)}" end |
.parameterized_locale(locale = I18n.default_locale) ⇒ Object
Parameterize the locale for a form field with Mobility
152 153 154 |
# File 'app/lib/locale_utility.rb', line 152 def parameterized_locale(locale = I18n.default_locale) locale.to_s.downcase.tr('-', '_') end |
.root_content_locale(locale = nil) ⇒ Object
Find the root language fallback
65 66 67 68 69 |
# File 'app/lib/locale_utility.rb', line 65 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
57 58 59 60 61 62 |
# File 'app/lib/locale_utility.rb', line 57 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 |