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’


Install

  • install dependency gem sishen-rtranslate:
sudo gem install sishen-rtranslate --source http://gems.github.com
  • install chinese_permalink plugin
script/plugin install git://github.com/flyerhzm/chinese_permalink.git

Example

  • 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
  • Generate migration
ruby script/generate chinese_permalink_migration (migration name) (table name) (permalink column name)

For example:

ruby script/generate chinese_permalink_migration add_permalink_to_posts posts

Or

ruby script/generate chinese_permalink_migration add_permalink_to_posts posts slug_url

3. Define ar to_param method


class Post < ActiveRecord::Base
  def to_param
    permalink
  end
end

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 &amp; Gamble', 'pg')
  end
end

Copyright © 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license

Add a Comment

Fd8b8caba0a3b73f21a6385001a6dadefb4e1372