Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails friendly id with non-Latin characters

I have a model which I use its friendly id as slug:

extend FriendlyId
friendly_id :slug_candidates, :use => :scoped, :scope => :account

def slug_candidates
  :title_and_sequence
end


def title_and_sequence
  slug = normalize_friendly_id(title)
      :
  # some login to add sequence in case of collision
      :
end

My problem is that when I use non-Latin chars (Arab, Hebrew,...) I get an empty slug. Is there any nice-and-easy solution?


UPDATE

Just to make my question clear, I would like to have the same behaviour as WordPress, which means:

+--------------------+----------------------------------------------------+
| Title              | url                                                |
+--------------------+----------------------------------------------------+
| Hello World!!      | /hello-world                                       |
+--------------------+----------------------------------------------------+
| Helló Világ        | /hello-vilag                                       |
+--------------------+----------------------------------------------------+
| שלום עולם          | /%D7%A9%D7%9C%D7%95%D7%9D-%D7%A2%D7%95%D7%9C%D7%9D |
+--------------------+----------------------------------------------------+
| مرحبا              | %D9%85%D8%B1%D8%AD%D8%A8%D8%A7                     |
+--------------------+----------------------------------------------------+

(both Arabic and Hebrew are translated in modern browsers to original and readable characters).

like image 255
guyaloni Avatar asked Nov 26 '25 13:11

guyaloni


1 Answers

There's a Rails API method for that transliterate

Example use:

transliterate('Ãrøskøbing')
# => "AEroskobing"

By default it only supports latin-based languages and Russian but you should be able to find rules for other alphabets as well (as explained in the linked doc)

EDIT
To achieve the same behaviour as wordpress you can simply use url encoding, as in example below

URI::encode('שלום') => "%D7%A9%D7%9C%D7%95%D7%9D"
like image 159
Mike Szyndel Avatar answered Nov 28 '25 03:11

Mike Szyndel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!