使Rails应用程序成为多语言的最佳实践 [英] Best practice in making Rails applications multilingual
问题描述
大家好:我刚刚开始学习Rails,目前我有一个问题。
我正在构建一个Rails网站,需要将其翻译为4语言。什么是最实际和方便的方法呢?
我读过的主要目标是为每种语言制作单独的文件夹并复制每种语言的所有视图。但是我的控制器里仍然有英文的路线
也是我关心的问题。我应该为4种不同的翻译视图提供4种不同的路线。
您建议如何处理这个问题?
感谢您的建议!
def创建
if user.save
flash:[:notice] = t(:user_was_successfully_created)
redirect_to users_users_path
else
render:new
end
end
您应该不是有4个不同的路径
Rails Internationalization I18n)API
查看此链接 http ://guides.rubyonrails.org/i18n.html
Rails国际化(I18n)API
< Ruby on Rails附带的Ruby I18n(国际化简写)gem(从Rails 2.2开始)提供了一个易于使用和可扩展的框架,可将您的应用程序转换为单个定制的la除英语以外的语言或在您的应用程序中提供多语言支持。
国际化过程通常意味着抽取所有字符串和其他特定于位置的位(例如日期或货币格式)。 本地化的过程意味着为这些位提供翻译和本地化格式。因此,在国际化Rails应用程序的过程中,您必须:
> 确保您支持i18n。
告诉Rails在哪里可以找到语言环境字典。
告诉Rails如何设置,保存和切换语言环境。
在本地化您的应用程序的过程中,您可能需要执行以下三件事:
替换或补充Rails的默认语言环境 - 例如日期和时间格式,月份名称,活动记录模型名称等。
应用程序中的关键词典中的抽象字符串 - 例如flash消息,视图中的静态文本等。
将产生的字典存储在某处。
本指南将引导您了解I18n API,并包含关于如何国际化Rails应用程序的教程开始。
阅读本指南后,您将知道:
Hello everyone :D I've just started learning Rails and currently I have one concern.
I am building a Rails web site which needs to be translated to 4 languages. What would be the most practical and convenient method to do it?
I've read the main goal would be to make separate folders for each language and copy all the views for each language. But I would still have notice
messages on English inside my controller so how would I handle that. Routes
are also my concern. Should I have 4 different routes for 4 different translated Views.
What do you recommend to handle this problem? I didn't find anything concrete online.
Thank you for your suggestions!
for your notice messages you can do
def create
if user.save
flash[:notice] = t(:user_was_successfully_created)
redirect_to users_users_path
else
render :new
end
end
you should not have 4 different routs
Rails Internationalization (I18n) API
take a look at this link http://guides.rubyonrails.org/i18n.html
Rails Internationalization (I18n) API
The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.
The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits.1
So, in the process of internationalizing your Rails application you have to:
Ensure you have support for i18n.
Tell Rails where to find locale dictionaries.
Tell Rails how to set, preserve and switch locales.
In the process of localizing your application you'll probably want to do the following three things:
Replace or supplement Rails' default locale — e.g. date and time formats, month names, Active Record model names, etc.
Abstract strings in your application into keyed dictionaries — e.g. flash messages, static text in your views, etc.
Store the resulting dictionaries somewhere.
This guide will walk you through the I18n API and contains a tutorial on how to internationalize a Rails application from the start.
After reading this guide, you will know:
这篇关于使Rails应用程序成为多语言的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!