Rails I18n - mindpin/docs GitHub Wiki
此Wiki主要涉及,如何给Rails项目中文支持,以及一些相应的规范。
## 如何使用 I18n 实现中文化 ### 快速使用 I18n 支持简体中文化 现在要对于 Rails 支持简体中文已经是很容易的事情,只需要修改两个文件: ``` # ./Gemfile 添加gem gem 'rails-i18n', '~> 4.0.0' ```
# ./config/application.rb
# 在 class Application < Rails::Application 内添加
config.i18n.default_locale = 'zh-CN'.to_sym
config.i18n.available_locales = 'zh-CN'.to_sym # 若要同时支持其他语言,可以用Array形式指定
做完这两项修改之后,常见的 表单错误提示 已经改为中文显示了。
当然,还有其他需要修改的,因为对于 model 以及其他的一些东西,系统还是不知道怎么翻译的。
模型(model) 中文化, 这里主要分为两块,一块为 模型(model) 名称中文化, 一块为 属性(attribute) 中文化。
具体可以参考以下代码。
# config/locales/zh-CN.yml
zh-CN:
# activerecord: # mysql, pg 为此形式
mongoid:
models:
user: 用户
comment: 评论
kc_courses/course: 课程 # 用于codestart 生成的 gem 那种在 module 下的 model
attributes:
user:
name: 用户名
comment:
content: 内容
kc_courses/course:
title: 标题
desc: 描述
attributes:
name: 名称 # 对于属性的通用中文化。例如有个新 model(Project) 对于他的属性 name ,中文化就叫 “名称”
添加支持前:
添加支持后:
可以看到form对于 属性(attribute) 、 模型(model) 名称都实现了中文化。
## 开发规范 ### codestart生成Gem/其余Rails Engine Gem I18n 中文化 由于 Gem 相对独立,中文化肯定也是独立的,尽可能坐到不影响其他 Gem ,以及 Rails 项目。
Rails Engine项目可以直接参考下列代码,实现中文化。(注意目录为codestart生成项目的根目录。Sample项目 或 引用此Gem的Rails项目,必须设置default_locale为zh-CN,才有效果)
# config/locales/zh-CN.yml
zh-CN:
mongoid:
models:
kc_courses/course: 课程
kc_courses/chapter: 章节
kc_courses/ware: 课件
attributes:
kc_courses/course:
title: 标题
desc: 描述
kc_courses/chapter:
title: 标题
desc: 描述
kc_courses/ware:
title: 标题
desc: 描述
若还需要对应其他依赖 Gem 设置中文化,请使用对应 Gem 的名称加上对应 .zh-CN.yml 来命名。
例如: enumerize
# config/locales/enumerize.zh-CN.yml
zh-CN:
enumerize:
user:
sex:
male: "男性"
female: "女性"
simple_form
# config/locales/simple_form.zh-CN.yml
zh-CN:
simple_form:
labels:
user:
username: '用户名'
password: '密码'
hints:
user:
username: '登录时候的需要用到的名称'
password: '请不要使用特殊字符.'
placeholders:
user:
username: '你的用户名'
password: '****'
include_blanks:
user:
age: '保密'
prompts:
user:
gender: '请选择你的性别'
参考一下例子:
# config/locales/labels.zh-CN.yml
zh-CN:
labels:
courses: # 控制器名
title: 课程名称
chapters_count: 章节数
wares_count: 课件数
submit: 提交课程
publish: 发布
published: 已发布
# 以下为通用/常用label
edit: 编辑
destroy: 删除
are_you_sure: 你确定吗?
app/views/courses/index.html.haml
%table.table.table-bordered.table-striped
%thead
%tr
%th= t('labels.courses.title')
%th= t('labels.courses.chapters_count')
%th= t('labels.courses.wares_count')
%th
%th
%th
%tbody
- @courses.each do |course|
%tr
%td
%a{href: (course_path(course))}= course.title
%td= course.chapters.count
%td= course.chapters.map{|chapter| chapter.wares.count}.sum
%td
%a{href: (edit_course_path(course))}= t('labels.edit')
%td
%a{href: (course_path(course)), data: {method: 'delete', confirm: t('labels.are_you_sure')}}= t('labels.destroy')
%td
- if course.published
= t('labels.courses.published')
- else
%a{href: (publish_course_path(course)), data: {method: 'post'}}= t('labels.courses.publish')
暂时能想到的东西不多,所以暂时只整理一些个人做过的尝试。
# config/locales/site.zh-CN.yml
zh-CN:
site:
default_title: 课程编排
title: '%{title} | 课程编排'
default_desc: 课程编排系统
desc: '%{desc} 课程编排系统'
**app/views/layouts/application.html.haml
!!!
%html{:lang => :zh}
%head
%meta{:'http-equiv' => 'Content-Type', :content => 'text/html; charset=utf-8'}
%meta{:name => 'viewport', :content => 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'}
%title= content_for(:title) ? t('site.title', title: yield(:title)) : t('site.default_title')
%meta{name:"description", content: (content_for?(:desc) ? t('site.desc', desc: yield(:description)) : t('site.default_desc')
= csrf_meta_tags
%body
%main{role: "main"}
.container
= yield
app/views/courses/index.html.haml
- content_for :title, '课程列表'
- content_for :desc, '课程列表,用于查看自己提交了的课程'
查看源代码,可以看到,已经按 i18n 设置巨型实现:
<title>课程列表 | 课程编排</title>
<meta content="课程列表,用于查看自己提交了的课程 课程编排系统" name="description">
而不设置 title 与 desc 的,会按我们设置的默认形式显示
<title>课程编排</title>
<meta content="课程编排系统" name="description">
大概就这么多。
如果还有什么其他好的建议,我们再不断完善。