大家可能注意到了,JavaEye新闻频道的链接在今天有了一些变化,原先一篇新闻的URL格式是这样:
[url]http://www.iteye.com/news/3585[/url]
而现在变成了:
[url=http://www.iteye.com/news/3585-the-new-strategy-for-the-maintenance-of-spring]http://www.iteye.com/news/3585-[color=red]the-new-strategy-for-the-maintenance-of-spring[/color][/url]
这种URL有个术语叫slugs url,有很多好处,比如:
1. 转贴链接的时候可以从url了解该内容,more user friendly
2. 搜索引擎优化(SEO)
3. ...
Rails有几个插件就提供了这个功能,比如acts_as_slugable:
[url]http://agilewebdevelopment.com/plugins/acts_as_slugable[/url]
但是做为中文网站,这些插件都不大合适,因为将中文标题作为slug url将会变成UTF8编码,失去了优化的意义,后来我们想到了用[url=http://code.google.com/apis/ajaxlanguage/]Google Translate[/url]提供的ajax api自动将标题翻译成英文,作为slugs url。
Google Translate的ajax API调用非常简单:
大家有在用Google Translate API做其他应用吗?欢迎交流
[url]http://www.iteye.com/news/3585[/url]
而现在变成了:
[url=http://www.iteye.com/news/3585-the-new-strategy-for-the-maintenance-of-spring]http://www.iteye.com/news/3585-[color=red]the-new-strategy-for-the-maintenance-of-spring[/color][/url]
这种URL有个术语叫slugs url,有很多好处,比如:
1. 转贴链接的时候可以从url了解该内容,more user friendly
2. 搜索引擎优化(SEO)
3. ...
Rails有几个插件就提供了这个功能,比如acts_as_slugable:
[url]http://agilewebdevelopment.com/plugins/acts_as_slugable[/url]
但是做为中文网站,这些插件都不大合适,因为将中文标题作为slug url将会变成UTF8编码,失去了优化的意义,后来我们想到了用[url=http://code.google.com/apis/ajaxlanguage/]Google Translate[/url]提供的ajax api自动将标题翻译成英文,作为slugs url。
Google Translate的ajax API调用非常简单:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("language", "1");
google.language.translate($F("news_title"), "zh", "en", function(result) {
if (!result.error) {
$("news_slug_url").value = slugify(result.translation);
}
});
function slugify(str) {
return str.toLowerCase().replace(/[^a-z0-9-_]+/g, '-').replace(/^-|-$/g, '');
}
</script>
大家有在用Google Translate API做其他应用吗?欢迎交流