Would it be possible to make Slug localizable?


(George Kaplan) #1

That would solve lots of issues with non-latin titles and comments.


(Sam Saffron) #2

We are using the AR slugger now, its meant to be localizable @danneu can you expand?


(Dan Neumann) #3

The slugger is locale-aware.

Let’s say we wanted to change how “ü” and “ö” are slugged for the Elvish locale.

# locales/elvish.yml
elvish:
  i18n:
    transliterate:
      rule:
        ü: "vilya"
        ö: "nuquerna"

Check it out:

I18n.locale = :en
Slug.for("ü-ö") # => "u-o"

I18n.locale = :elvish
Slug.for("ü-ö") # => "vilya-nuquerna"

Does that answer your question, @georgekaplan195?


(George Kaplan) #4

How do I set a different locale for Discourse?


(Dan Neumann) #5

Yeah, halfway through my last post I realized I’m talking about it from a developer perspective and not a user perspective.

I don’t think Discourse provides a simple option for it yet, and if it doesn’t, the feature would take only a moment to add.

In other words, if you were to deploy a Discourse forum for your own website, I’m sure changing the locale will be as easy as editing settings(:locale, :en) to settings(:locale, :elvish) or selecting elvish from an admin panel.


(George Kaplan) #6

yes, should be that easy.

If I want to experiment now, what do I do? I have forked Discourse already, but don’t see any option for locale in the admin settings. Any tips?


(Dan Neumann) #7

Sure, you’ll just have to edit it directly in the framework.

Check out the config/application.rb file.

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :elvish

I uncommented the last line and changed it to :elvish.

Basically, your next step would be to copy the server.en.yml and client.en.yml files and ensure you change the first en: in the file to elvish:.

But now you have to translate the entire app (and I’m sure Discourse would love if you shared that with the repository!)

However, if you are happy with the English interface and just want to change some characters in the slug, then just nest the code in my previous post under the en: key in the config/locales/server.en.yml file. I think it’ll just override the English default.


(George Kaplan) #8

Le fael. Thank you very much. [quote=“danneu, post:7, topic:3071”]
But now you have to translate the entire app (and I’m sure Discourse would love if you shared that with the repository!)
[/quote]
yes, will do :smile:


(Régis Hanol) #9

Just to keep this topic up to date, @codinghorror blogged about Localizing Discourse.


(Erick Guan) #10

Localized for Chinese

I don’t know whether it’s possible to generate proper slug for Chinese with AR slugger. I guess no.

Well, I think it’s possible to bring in stringex gem.

It can dump 习近平:中企承建港口电站等助斯里兰卡发展 into xi-jin-ping-zhong-qi-cheng-jian-gang-kou-dian-zhan-deng-zhu-si-li-lan-qia-fa-zhan which is a localized symbolic notation for Chinese. Putting Chinese Unicode into slug is bad, but in this form, it would be great.

After did some experiments, it actually breaks something. Still need some configuration.

  1) Search Chinese search finds chinese topic based on title
     Failure/Error: Search.execute('社区指南').posts.first.id.should == post.id
     NoMethodError:
       undefined method `id' for nil:NilClass
     # ./spec/components/search_spec.rb:299:in `block (3 levels) in <top (required)>'

  2) Slug handles a.b.c properly
     Failure/Error: Slug.for("a.b.c").should == "a-b-c"
       expected: "a-b-c"
            got: "abc" (using ==)
     # ./spec/components/slug_spec.rb:21:in `block (2 levels) in <top (required)>'

  3) Slug replaces symbols
     Failure/Error: Slug.for('evil#trout').should == 'evil-trout'
       expected: "evil-trout"
            got: "evil-number-trout" (using ==)
     # ./spec/components/slug_spec.rb:17:in `block (2 levels) in <top (required)>'

  4) Slug doesn't generate slugs that are just numbers
     Failure/Error: Slug.for('電車男 2').should be_blank
       expected blank? to return true, got false
     # ./spec/components/slug_spec.rb:51:in `block (2 levels) in <top (required)>'

  5) Slug handles double dots right
     Failure/Error: Slug.for("a....b.....c").should == "a-b-c"
       expected: "a-b-c"
            got: "a-dot-dot-dot-b-dot-dot-dot-c" (using ==)
     # ./spec/components/slug_spec.rb:25:in `block (2 levels) in <top (required)>'

  6) Slug strips leading punctuation
     Failure/Error: Slug.for("...hello").should == "hello"
       expected: "hello"
            got: "dot-dot-dot-hello" (using ==)
     # ./spec/components/slug_spec.rb:33:in `block (2 levels) in <top (required)>'

  7) Slug strips trailing punctuation
     Failure/Error: Slug.for("hello...").should == "hello"
       expected: "hello"
            got: "hello-dot-dot-dot" (using ==)
     # ./spec/components/slug_spec.rb:29:in `block (2 levels) in <top (required)>'

  8) Category non-english characters creates a blank slug, this is OK.
     Failure/Error: category.slug.should be_blank
       expected blank? to return true, got false
     # ./spec/models/category_spec.rb:183:in `block (3 levels) in <top (required)>'

  9) Category slug would be a number creates a blank slug
     Failure/Error: category.slug.should be_blank
       expected blank? to return true, got false
     # ./spec/models/category_spec.rb:192:in `block (3 levels) in <top (required)>'

Question

Is that good?

Sometimes it would be large, so need to truncate it into proper size.


(Sam Saffron) #11

Breaking search is not good (first broken spec), but overall the approach of using this gem to generate slugs (if chinese is selected) seems like a sane approach.

Another option would be to upstream fixes to Rails itself.


(Erick Guan) #12
 # The Rails core team has stated patches for the inflections library will not
 # be accepted in order to avoid breaking legacy applications which may be
 # relying on errant inflections. If you discover an incorrect inflection and
 # require it for your application or wish to define rules for languages other
 # than English, please correct or add them yourself (explained below).

Just found these comments from inflector. It was added at Sep 14, 2012.

I would chose gem.