使Rails应用程序多语言的最佳实践

use*_*562 10 html multilingual html5 ruby-on-rails ruby-on-rails-4

大家好:D我刚刚开始学习Rails,目前我有一个问题.

我正在构建一个Rails网站,需要翻译成4种语言.最实用,最方便的方法是什么?

我读过主要目标是为每种语言创建单独的文件夹并复制每种语言的所有视图.但是notice我的控制器里面仍然会有关于英语的消息,所以我该如何处理呢.Routes也是我关注的问题.我应该为4种不同的翻译视图提供4种不同的路线.

您建议如何处理此问题?我没有在网上找到任何具体的东西.

谢谢你的建议!

MZa*_*oza 2

对于您的通知消息,您可以执行以下操作

\n\n
  def create\n    if user.save\n      flash[:notice] = t(:user_was_successfully_created)\n      redirect_to users_users_path\n    else\n      render :new\n    end\n  end\n
Run Code Online (Sandbox Code Playgroud)\n\n

应该有4条不同的路线

\n\n

Rails 国际化 (I18n) API

\n\n

看看这个链接http://guides.rubyonrails.org/i18n.html

\n\n

Rails 国际化 (I18n) API

\n\n

Ruby I18n(国际化的简写)gem 随 Ruby on Rails(从 Rails 2.2 开始)一起提供,提供了一个易于使用且可扩展的框架,用于将您的应用程序翻译为英语以外的单一自定义语言或提供多语言在您的应用程序中提供支持。

\n\n

“国际化”过程通常意味着从应用程序中抽象出所有字符串和其他区域特定位(例如日期或货币格式)。“本地化”过程意味着为这些位提供翻译和本地化格式。1

\n\n

因此,在国际化 Rails 应用程序的过程中,您必须:

\n\n
Ensure you have support for i18n.\nTell Rails where to find locale dictionaries.\nTell Rails how to set, preserve and switch locales.\n
Run Code Online (Sandbox Code Playgroud)\n\n

在本地化应用程序的过程中,您可能需要执行以下三件事:

\n\n
Replace or supplement Rails\' default locale \xe2\x80\x94 e.g. date and time formats, month names, Active Record model names, etc.\nAbstract strings in your application into keyed dictionaries \xe2\x80\x94 e.g. flash messages, static text in your views, etc.\nStore the resulting dictionaries somewhere.\n
Run Code Online (Sandbox Code Playgroud)\n\n

本指南将引导您了解 I18n API,并包含有关如何从头开始国际化 Rails 应用程序的教程。

\n\n

阅读本指南后,您将了解:

\n