-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support translations for ActiveRecord models
Problem: Developers are unable to display their ActiveRecord classes in the UI by any other name than the titleized class name. For example, if a team is building an admin dashboard that will be used in Russian, they may have ActiveRecord models with English names, that need to be translated before they're displayed in the UI. Solution: Rails has support for [translating ActiveRecord Models][1], which relies on defining I18n translations under the `activerecord.models` namespace. Rails expects translations in the form: ```yaml en: activerecord: models: user: one: Customer other: Customers ``` The separate `one` and `other` keys allow Rails to display the correct plural or singular form of the word, depending on the context. We've used Rails' built-in `Foo.model_name.human` method to correctly pull the translations out of I18n, with sane fallbacks if the translation is not defined. [1]: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
- Loading branch information
Showing
7 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Administrate::ApplicationHelper do | ||
describe "#display_resource_name" do | ||
it "defaults to the plural of the model name" do | ||
displayed = display_resource_name(:customer) | ||
|
||
expect(displayed).to eq("Customers") | ||
end | ||
|
||
it "handles string arguments" do | ||
displayed = display_resource_name("customer") | ||
|
||
expect(displayed).to eq("Customers") | ||
end | ||
|
||
it "handles plural arguments" do | ||
displayed = display_resource_name(:customers) | ||
|
||
expect(displayed).to eq("Customers") | ||
end | ||
|
||
context "when translations are defined" do | ||
it "uses the plural of the defined translation" do | ||
translations = { | ||
activerecord: { | ||
models: { | ||
customer: { | ||
one: "User", | ||
other: "Users", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
with_translations(:en, translations) do | ||
displayed = display_resource_name(:customer) | ||
|
||
expect(displayed).to eq("Users") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
RSpec.configure do |config| | ||
config.include AbstractController::Translation | ||
|
||
def with_translations(locale, translations) | ||
original_backend = I18n.backend | ||
|
||
I18n.backend = I18n::Backend::KeyValue.new({}, true) | ||
I18n.backend.store_translations(locale, translations) | ||
|
||
yield | ||
ensure | ||
I18n.backend = original_backend | ||
end | ||
end |