Module: CountryStateSelect::Localization
- Defined in:
- lib/country_state_select/localization.rb
Overview
Best-effort translation of country names for the current I18n.locale.
Two lookup strategies, tried in order, so neither is a hard dependency:
1. The `countries` gem (ISO3166::Country), if the host app already
depends on it — it ships translations for all ISO locales.
2. A `country_state_select.countries.<ISO_CODE>` key in the host
app's own locale files, for apps that want to supply their own.
Falls back to the English name from the data source when neither
strategy has a translation, so localize_names is always safe to turn
on even with sparse translation coverage.
Class Method Summary collapse
- .country_name(code, english_name) ⇒ Object
- .via_countries_gem(code) ⇒ Object
- .via_i18n(code) ⇒ Object
Class Method Details
.country_name(code, english_name) ⇒ Object
19 20 21 22 23 |
# File 'lib/country_state_select/localization.rb', line 19 def country_name(code, english_name) return english_name unless CountryStateSelect.configuration.localize_names via_countries_gem(code) || via_i18n(code) || english_name end |
.via_countries_gem(code) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/country_state_select/localization.rb', line 25 def via_countries_gem(code) return nil unless defined?(::ISO3166::Country) ::ISO3166::Country.new(code.to_s)&.translations&.[](I18n.locale.to_s) rescue StandardError nil end |
.via_i18n(code) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/country_state_select/localization.rb', line 33 def via_i18n(code) key = "country_state_select.countries.#{code}" translated = I18n.t(key, default: nil) translated if translated.present? rescue StandardError nil end |