Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 신품 ?

like image 872
Richard Lapiš Avatar asked May 31 '13 11:05

Richard Lapiš


2 Answers

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.

like image 79
Jesse Wolgamott Avatar answered Nov 02 '22 11:11

Jesse Wolgamott


Not tried these yet but I'm going to:

  • https://github.com/makimoto/romaji
  • https://github.com/aelaa/hangul
  • https://github.com/janx/ruby-pinyin

I'll report back when I've tried them.

like image 36
Dominic Sayers Avatar answered Nov 02 '22 10:11

Dominic Sayers