How to return api data translated with i18n?

0

My API returns serialized data with active_model_serializers , some of these attributes are translated into the frontend using angular-translate .

The dictionary containing the translations is growing, I want to translate this data into the backend and to return this data translated.

The form I found was through the layer of serializers defining methods with each attribute and translating them.

I wish the opinion of others who have done something similar in their projects.

Thank you

    
asked by anonymous 19.02.2017 / 20:24

2 answers

0

Using the ruby eval command you can create the methods dynamically within the application ...

Create translations normally, and use this method within your class to return them ... For example ...

name = ['traducao 1', 'traducao 2']
name.each do |name|
 eval("def #{name}?
   I18n.t('name')
 end")
end

I think this helps, if I think of another solution put here, thanks !!

    
20.04.2017 / 16:47
0

The way I found to solve the question, was defining method by method for each attribute. See the code below:

# frozen_string_literal: true
class User
  class RolesSerializer < ApplicationSerializer # :nodoc:
    attributes :name

    def name
      I18n.t object.name, scope: 'roles'
    end
  end
end
    
19.02.2017 / 20:34