h1. ChinesePermalink
This plugin adds a capability for ar model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate.
The permalink will be composed of id and the english value translated from chinese text.
For exmpale, title of post is '我是中国人', permalink of post is '1-i-am-a-chinese'
h2. Install
sudo gem install chinese_permalink
h2. Config
As google translator api begins to charge, I decide to use bing translator api, it's free. You have to add a config file to tell chinese_permalink what's your bing app id in config/chinese_permalink.yml
bing:
app_id: xxx
language: zh-cht (chinese traditional, default is zh-chs, chinese simplified)
h2. Example
1. Define chinese_permalink to model
class Post < ActiveRecord::Base
# create permalink by chinese title, default permalink column is "permalink"
chinese_permalink :title
end
Or
class Post < ActiveRecord::Base
# create permalink by chinese category and title
chinese_permalink [:category, :title]
end
Or
class Post < ActiveRecord::Base
# create permalink by chinese title, store permalink to column "slug_url"
chinese_permalink :title, :permalink_field => :slug_url
end
2. Generate migration
script/generate chinese_permalink_migration (table name) (permalink column name)
For example:
script/generate chinese_permalink_migration posts
Or
script/generate chinese_permalink_migration posts slug_url
3. Define ar to_param method
class Post < ActiveRecord::Base
def to_param
"#{id}-#{permalink}"
end
end
h2. Advance
You can add before_methods and after_methods to meet your business, for example:
class Post < ActiveRecord::Base
chinese_permalink :title, :before_methods => :parse_c_sharp
def parse_c_sharp(permalink)
permalink.gsub('C#', 'c-sharp')
end
end
class Post < ActiveRecord::Base
chinese_permalink :title, :after_methods => :parse_pg
def parse_pg(permalink)
permalink.gsub('Procter & Gamble', 'pg')
end
end
Copyright (c) 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license