FriendlyID韩国蛞蝓 [英] FriendlyID Korean slugs
问题描述
当我用韩文添加文章时,标题例如:신품
When I add article in Korean language with title e.g.: 신품
FriendlyID gem 创建空白 slug 并且 url 就像 /8
... 所以这是 ID.看这个链接:http://www.srecipe.kr.com/articles/8
FriendlyID gem creates blank slug and url is like /8
... so this is ID. Look at this link: http://www.srecipe.kr.com/articles/8
其他语言有效.
如何从 신품
获取映射到拉丁字母(如/this-is-url)的 url?
How can I get url which is mapped to latin letters like /this-is-url from 신품
?
推荐答案
与所有这些固定链接解决方案一样,友好 ID 使用 parameterize
方法将字符串转换为 URL 安全字符串.像这样:
Like all of these permalink solutions, friendly ID uses the parameterize
method to convert a string into a URL safe string. like so:
require 'active_support/all'
puts "Oh Hai There".parameterize
=> oh-hai-there
当您使用非 ASCII 字符串时会出现问题,该字符串将参数化替换为空字符串,从而导致您的问题:
The problem comes in when you use non ASCII strings, which parameterize replaces with an empty string, causing your problem:
# encoding: UTF-8
require 'active_support/all'
puts "신품".parameterize
=>
ActiveSupport 提供了一种通过 transliterate
方法将非 ASCII 字符串更改为近似值的方法.
ActiveSupport provides a way to change non ASCII strings to a close approximate via the transliterate
method.
# encoding: UTF-8
require 'active_support/all'
include ActiveSupport::Inflector
puts transliterate("Ærøskøbing")
=> AEroskobing
但是,如果它不知道一个字符,它会默认为 ??
But, if it doesn't know about a character, it'll default to ??
# encoding: UTF-8
require 'active_support/all'
include ActiveSupport::Inflector
puts transliterate "신품"
=> ??
但是,您可以告诉音译如何处理字符.所以在 Rails 模型中
But, you can tell transliterate how to handle the characters. So in a Rails model
# Store the transliterations in locales/en.yml
en:
i18n:
transliterate:
rule:
신: "abc"
품: "def"
puts transliterate "신품"
=> "abcdef"
因此,您可以使用 transliterate(title).parameterize
而不是参数化.如果您将韩文字母转换为音译部分,您就接近黄金了.
So, you can use transliterate(title).parameterize
instead of just parameterize. And if you get the korean alphabet into transliterate section, you're close to golden.
这篇关于FriendlyID韩国蛞蝓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!