Reference:
http://wiki.globalize-rails.org/globalize/show/Example+Application
http://www.artweb-design.de/2006/11/10/get-on-rails-with-globalize-comprehensive-writeup
http://hideto.javaeye.com/blog/82243
1. 安装 globalize_plugin
1.1 ruby script/plugin install
http://svn.globalize-rails.org/svn/globalize/trunk
或直接下载
http://globalize.artweb-design.de/20070316/globalize_trunk.tar.gz
然后解压拷贝到 vendor/plugin 下
vendor/plugin 下会多出一个 trunk 目录,为了易于识别,我将其改名为 globalize
1.2 rake globalize:setup
或
ruby script/generate globalize
rake db:migrate
db/migrate 目录下多出一个globalize plugin 的 db migrate 脚本
xxx_globalize_migration.rb
数据库中多出3个表, globalize_countries, globalize_languages,
globalize_translations
2. 更改配置文件
2.1 config/environment.rb
# Include your application configuration below
$KCODE = 'u'
require 'jcode'
include Globalize
Locale.set_base_language 'zh-CN'
Locale.set 'zh-CN'
LOCALES = {'en' => 'en-US',
'zh' => 'zh-CN'}.freeze
2.2 config/database.yml
encoding: utf8
2.3 config/routes.rb
map.connect ':locale/:controller/:action/:id'
3. 测试 & built-in?
开启ruby console: ruby script/console
输入
include Globalize
Locale.set "en-US"
Locale.set_translation("foo", "bar")
ViewTranslation.find(:first, :conditions => "tr_key =
'foo'").built_in?
如果有如下错误
NoMethodError: undefined method `built_in?' for
#<Globalize::ViewTranslation:0x467d49c>
from D:/dev/ror/ruby/lib/ruby/gems/1.8/gems/
activerecord-1.15.5/lib/active_record/base.rb:1860:in `method_missing'
from (irb):5
打开 vendor/plugins/globalize/tasks/data.rake
修改 t.column :built_in, :boolean, :default => true 为
t.column :built_in, :boolean#, :default => true
重新安装globalize plugin
rake globalize:teardown
rake globalize:setup
在 ruby console 中输入:
include Globalize
ViewTranslation.update_all "built_in = true"
看到被修改的记录数返回到ruby console上, 表示更改成功
Now it should work correctly: all translations that already exist in
the table should be marked as built-in and all new translations added
by you won't be anymore.
Note that due to the unorthodox way Globalize handles the database
(without migrations), this change won't be reflected in schema.rb.
4. 更改应用程序
ApplicationController.rb
before_filter :set_charset
before_filter :set_locale
def set_charset
logger.debug "======in application controller set_charset"
content_type = headers["Content-Type"] || "text/html"
if /^text\//.match(content_type)
headers["Content-Type"] = "#{content_type};
charset=utf-8"
end
end
def set_locale
logger.debug "======in application controller set_locale"
default_locale = 'zh-CN'
request_language = request.env['HTTP_ACCEPT_LANGUAGE']
request_language = request_language.nil? ? nil :
request_language[/[^,;]+/]
@locale = params[:locale] || session[:locale] || request_language
|| default_locale
session[:locale] = @locale
begin
Locale.set @locale
rescue
Locale.set default_locale
end
true
end
页面
<%= link_to "en", {:controller =>
controller.controller_name, :action => controller.action_name, :locale
=> 'en', :id => params[:id]} %>
<%= link_to "zh", {:controller =>
controller.controller_name, :action => controller.action_name, :locale
=> 'zh', :id => params[:id]} %>
5. 完成
测试方式:
5.1
insert into globalize_translations (type, tr_key, table_name,
item_id, facet, language_id, pluralization_index, text, namespace)
values ('ViewTranslation', 'Home', '', NULL, '', 7484, 1, 'Home',
NULL);
5.2 在translate admin 中修改 Home 对应的中文内容 为 "首页"
在页面中把需要转换的单词如 "Home" 改为 "<%= "Home".t%> 即可看到效果
注:
1. 数据库字符集必须是utf8
CREATE DATABASE IF NOT EXISTS cor_43things CHARACTER SET utf8;
GRANT ALL PRIVILEGES ON cor_43things.* TO 43things@"%" IDENTIFIED BY
"43things";
GRANT ALL PRIVILEGES ON cor_43things.* TO 43things@localhost
IDENTIFIED BY "43things";
2. 查询数据库字符集
show variables like '%char%';
相关代码已经check in,但目前只是转换了"Home", 机制有了维护起来就容易了,接下来我会完善那个translate admin页面,
咱们可以使用这个管理程序维护需要转换的资源