ruby-on-railsjbuildermobility

Generating an I18n attribute in a json jbuilder flow


The following, in a context where the Mobility gem is used for handling translation of values.

      json.label transaction.categoryminor.name

is generating a value, which would be blank as Mobility populates translations only in the string and text translations table. In any case, one wants to access the values indifferently.

Assuming Consumer belongs_to :idiom and the idiom table has a code value for the idiom.

Note, variations on the syntax for strong params were attempted to no avail:

translated_params = (I18n.available_locales.map do |l|
        [:"name_#{Mobility.normalize_locale(l)}"

How can this name attribute be presented in the json response with the proper idiom?


Solution

  • One way to fetch values in a locale is to pass the locale option to the getter method

    json.label transaction.categoryminor.name(locale: I18n.locale)

    Or for getting all translations

    json.translated_params I18n.available_locales do |locale|
      json.set! locale, transaction.categoryminor.name(locale:locale)
    end