Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleI18n: Handle regional locales #965

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/friendly_id/simple_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module FriendlyId

In order to use this module, your model must have a slug column for each locale.
By default FriendlyId looks for columns named, for example, "slug_en",
"slug_es", etc. The first part of the name can be configured by passing the
`:slug_column` option if you choose. Note that the column for the default locale
must also include the locale in its name.
"slug_es", "slug_pt_br", etc. The first part of the name can be configured by
passing the `:slug_column` option if you choose. Note that the column for the
default locale must also include the locale in its name.

This module is most suitable to applications that need to support few locales.
If you need to support two or more locales, you may wish to use the
Expand All @@ -26,10 +26,12 @@ def self.up
t.string :title
t.string :slug_en
t.string :slug_es
t.string :slug_pt_br
t.text :body
end
add_index :posts, :slug_en
add_index :posts, :slug_es
add_index :posts, :slug_pt_br
end

### Finds
Expand All @@ -40,6 +42,8 @@ def self.up
Post.friendly.find("la-guerra-de-las-galaxias")
I18n.locale = :en
Post.friendly.find("star-wars")
I18n.locale = :"pt-BR"
Post.friendly.find("guerra-das-estrelas")

To find a slug by an explicit locale, perform the find inside a block
passed to I18n's `with_locale` method:
Expand Down Expand Up @@ -98,7 +102,13 @@ def slug=(value)

module Configuration
def slug_column
"#{super}_#{I18n.locale}"
"#{super}_#{locale_suffix}"
end

private

def locale_suffix
I18n.locale.to_s.underscore
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def up
add_column :journalists, "slug_en", :string
add_column :journalists, "slug_es", :string
add_column :journalists, "slug_de", :string
add_column :journalists, "slug_fr_ca", :string

# This will be used to test relationships
add_column :books, :author_id, :integer
Expand Down
10 changes: 10 additions & 0 deletions test/simple_i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ def setup
test "friendly_id should return the current locale's slug" do
journalist = Journalist.new(:name => "John Doe")
journalist.slug_es = "juan-fulano"
journalist.slug_fr_ca = "jean-dupont"
journalist.valid?
I18n.with_locale(I18n.default_locale) do
assert_equal "john-doe", journalist.friendly_id
end
I18n.with_locale(:es) do
assert_equal "juan-fulano", journalist.friendly_id
end
I18n.with_locale(:"fr-CA") do
assert_equal "jean-dupont", journalist.friendly_id
end
end

test "should create record with slug in column for the current locale" do
Expand Down Expand Up @@ -114,6 +118,12 @@ class ConfigurationTest < TestCaseClass
end
end

test "should add locale to slug column for a locale with a region subtag" do
I18n.with_locale :"fr-CA" do
assert_equal "slug_fr_ca", Journalist.friendly_id_config.slug_column
end
end

test "should add locale to non-default slug column and non-default locale" do
model_class = Class.new(ActiveRecord::Base) do
self.abstract_class = true
Expand Down