Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ruby on rails - FriendlyID Korean slugs

When I add article in Korean language with title e.g.: ??

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

Other languages work.

How can I get url which is mapped to latin letters like /this-is-url from ?? ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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 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 "??"
=> ??

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"

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...