i18翻译link_to文本栏

时间:2015-11-20 11:16:22

标签: ruby-on-rails internationalization

您好,我需要设计一个以英语和西班牙语运行的应用程序。在rails中有任何选项,如按钮,用于翻译link_to标签文本 我正在寻找类似于此

的选项
helpers:
submit:
  # This will be the default ones, will take effect if no other
  # are specifically defined for the models.
  create: "Créer %{model}"
  update: "Modifier %{model}"

3 个答案:

答案 0 :(得分:1)

全部是described here

您需要将西班牙语和英语翻译保存在:config / locales / es.yml和config / locales / en.yml中。

对于链接文本,您可以执行以下操作:

es:
  links:
    home: "Casa"

然后你可以打电话:

<%= link_to t('links.home'), root_path %>

答案 1 :(得分:0)

您只需要修复您的语言环境文件(缩进不正确):

helpers:
  submit:
    # This will be the default ones, will take effect if no other
    # are specifically defined for the models.
    create: "Créer %{model}"
    update: "Modifier %{model}"

答案 2 :(得分:0)

创建两个文件:config/locales/es.yml&amp; config/locales/en.yml并在自己的文件中定义Spenish和英语内容。

然后定义您的链接:

<% link_to t('path.to.key'), your_url %>

我在我的应用程序中完成了类似的工作,用于切换英语&amp; Swahilli。

切换语言代码:

查看:

<div class="pull-right">
  <% [:en, :swa].each do |language| %>
    <%= link_to language.to_s.upcase, change_language_path(language), class: "btn btn-info #{'disabled' if language == I18n.locale}" %>
  <% end %>
</div>

控制器:

def change_language
  cookies[:language] = params[:language]

  redirect_to :back
end

Route.rb

match 'change_language/:language' => 'my_controller#change_language', as: 'change_language'