I18n Country Translations
Country name translations for Rails and the i18n gem. If you're doing anything with country names and translations, there's no need to reinvent the wheel — just use this gem and skip the hassle of managing translations for each locale yourself.
Requirements
- Ruby >= 3.1
- Rails >= 7.2 (or
railties>= 7.2)
Installation
Add to your Gemfile:
gem 'i18n-country-translations'Translation data is provided by the i18n-country-translations-data gem, which is installed automatically as a dependency.
Usage
I18n.t(:US, scope: :countries)
# => "United States"
I18n.locale = :de
I18n.t(:US, scope: :countries)
# => "Vereinigte Staaten"
I18n.locale = :ja
I18n.t(:JP, scope: :countries)
# => "日本"Or in Rails views:
t(:US, scope: :countries)Supported Locales
168 locales sourced from Unicode CLDR, including all ISO 639-1 languages plus additional CLDR-supported locales.
Supported Country Codes
257 territory codes following ISO 3166-1 alpha-2 (all 248 officially assigned codes), plus these additional territory codes from Unicode CLDR: AC, CP, DG, EA, EU, IC, QO, TA, UM, XK.
How It Works
This gem uses a Railtie to automatically load translations after Rails initializes. Translations are loaded from the i18n-country-translations-data gem via I18n.backend.store_translations, scoped under countries:.
If your app sets config.i18n.available_locales, only the matching locales will be loaded.
Contributing
Translation data lives in the i18n-country-translations-data repo. To add or fix translations, submit a pull request there.
Running tests
bundle install
bundle exec rspecAlso Available for JavaScript
-
i18n-country-translations-js— The same translations for JavaScript/TypeScript projects
Related
- i18n_country_select — A Rails select helper for country dropdowns using these translations
- i18n-timezones — Translations for Rails default timezones
License
MIT or GPL-3.0