How to return data from the i18n translated api?

Asked

Viewed 57 times

0

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

The dictionary containing the translations is increasing, I wish to make the translation of this data in the backend and already return translated this data.

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

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

Thank you

2 answers

0


Using the ruby command eval you can create 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 it helps, if I think of another solution put here, thanks!!

0

The way I found to resolve the issue was by defining method by method for each attribute. See below the code:

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

    def name
      I18n.t object.name, scope: 'roles'
    end
  end
end
  • I would very much like a more effective way to address this issue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.