Dongfeng Gu
Dongfeng Gu

Reputation: 23

rails i18n lazy lookup for module controller

If I have a controller with module Api::V1 like this

class Api::V1::UsersController < Api::ApiController
  def index
    print I18n.t('.messages')
    ...
  end
end

how can I write the localization file en.yml so that I can utilize lazy lookup feature inside my controller file?

I have tried

en:
  api/v1/users:
    index:
      messages: test message

But it was not working. Any kind of suggestion will be appreciated. Thank you!

Upvotes: 2

Views: 1369

Answers (3)

heber gentilin
heber gentilin

Reputation: 155

Fixed this reusing yaml translation block:

pt-BR:
  lazy: &lazy
    destroy:
      success: 'Competição removida com sucesso!'
    create:
      success: 'Competição foi criada com sucesso!'
    update:
      success: 'Competição foi alterada com sucesso!'
  swimming_competitions: 
    <<: *lazy
  admin:
    swimming_competitions:
      <<: *lazy

Upvotes: 0

Michael Dahl
Michael Dahl

Reputation: 389

First use

en:
  api:
    users:

etc. as suggested by @sajin.

Then use print t('.messages') instead of print I18n.t('.messages') without the I18n to use the controllers implementation of translate instead of the general one from I18n.

Upvotes: 2

Sajin
Sajin

Reputation: 1638

I'm not sure about this. But can you try

en:
  api:
    v1:
      users:
        index:
          messages: test message

Upvotes: 1

Related Questions