Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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.

branch: master
README.textile

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

sudo gem install chinese_permalink

Setup

As google translator api begins to charge, I decide to use bing translator api, it’s free.

First, subscribe to Microsoft Translator on Windows Azure Marketplace.

Then, fill your Marketplace application info in config/chinese_permalink.yml. You can create an app on Marketplace Developer page.


bing:
  client_id: APP_CLIENT_ID
  client_secret: APP_CLIENT_SECRET
  language: zh-cht # (chinese traditional, default is zh-chs, chinese simplified)

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

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

Something went wrong with that request. Please try again.