JsonLocale provides a simple api for managing translated data inside a json field by providing convinient methods to manage the translation values, along with fallbacks and some conventions for drying your code.
Add this line to your application's Gemfile:
gem 'json_locale'
And then execute:
bundle
Or install it yourself as:
gem install json_locale
- customizable default suffix
First we need to configure which locales will be available
Here is a list of available configurations:
- available_locales
- before_set (Proc as callback)
- default_locale (may also be Proc)
- suffix
- allow_blank
- fallback
- set_missing_accessor
JsonLocale::Translates.configure do |config|
config.default_locale = 'es'
config.available_locales = ['en', 'es', 'de']
end
Let's build an example where you have an attribute Country.name_translations
of type json that you want translate in multiple locales.
This gem requires that the name of the method to be translated have a suffix to differentiate the main method from generated methods. The suffix can be customized by the suffix param on the #translates method
class Country
include JsonLocale::Translates
translates :name_i18n, {suffix: '_i18n', set_missing_accessor: true}
def initialize(name_i18n:)
@name_i18n = name_i18n
end
end
record = Country.new(name_i18n: {'en' => 'Germany', 'es' => 'Alemania'})
# from default
record.name # => 'Germany'
# passing locale as param
record.name(locale: 'es') => 'Alemania'
record.name(locale: 'de', fallback: ['es']) => 'Alemania'
# specific locale by method name
record.name_en => 'Germany'
record.name_de(fallback:['es']) => 'Germany'
# set value for a specific locale
record.set_name_es('') #removes the value due to allow_blank: false
record.set_name_es('', {allow_blank: true}) #override allow_empty option
# bulk set locales
record.name_i18n => {'en' => 'Germany', 'es' => ''}
#used to set multiple translations at a time (merges)
record.set_name_i18n({es: 'Canadá', en: 'Canada'})
fallback: false => # no fallback
fallback: :any => # fall back first to the default locale, then to any other locale
fallback: ['sv'] => # explicitly declare fallbacks as an array of any length