-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
Add support to translate model_name #462
Add support to translate model_name #462
Conversation
7e725b4
to
7907b26
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a test case is missing and I would prefer english in the tests. Thanks. Nice job 👍
message = I18n.translate(keys.shift, variables.merge(scope: :unauthorized, default: keys + [''])) | ||
message.blank? ? nil : message | ||
end | ||
|
||
def translate_subject(subject) | ||
klass = (subject.class == Class ? subject : subject.class) | ||
if klass.respond_to?(:model_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in which case the class does not respond to model_name
? I see two cases here but only one test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in which case the class does not respond to model_name ?
I think there are cases which the class does not respond to model_name
because in following spec use Array as ability subject. So I wrote this method considering cases which doesn't respond to model_name
.
https://github.com/CanCanCommunity/cancancan/blob/develop/spec/cancan/ability_spec.rb#L498
I see two cases here but only one test.
Test case which does not respond to model_name
has wrote already here:
https://github.com/CanCanCommunity/cancancan/blob/develop/spec/cancan/ability_spec.rb#L496
spec/cancan/ability_spec.rb
Outdated
|
||
I18n.default_locale = :ja | ||
I18n.backend.store_translations :ja, | ||
activemodel: { models: { account: 'アカウント' } }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please use english? You can specify it as ja
but please use english words anyway.
Maybe also add translations in another language en
to see that they actually differ in your result.
I18n.backend.store_translations :de, activemodel: { models: { account: 'english name' } }
I18n.backend.store_translations :ja, activemodel: { models: { account: 'japanese name' } }
expect(...).to eq('japanese name...')
spec/cancan/ability_spec.rb
Outdated
activemodel: { models: { account: 'アカウント' } }, | ||
unauthorized: { update: { all: '%{subject}の更新権限がありません' } } | ||
expect(@ability.unauthorized_message(:update, Account)).to eq('アカウントの更新権限がありません') | ||
# rubocop:enable Style/FormatString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be removed.
… into translate_model_name
spec/cancan/ability_spec.rb
Outdated
unauthorized: { update: { all: '%{subject}' } } | ||
|
||
I18n.default_locale = :en | ||
expect(@ability.unauthorized_message(:update, Account)).to eq('english name') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please use a
I18n.with_locale(:fr) do
end
block? this avoids collateral effects and allow us to avoid the line 494
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coorasse I want to fix CI failures below. Is it better to suppress Rubocop offence (
https://travis-ci.org/CanCanCommunity/cancancan/jobs/306370981 |
both of those solutions are not fixes. we probably need to split the module. |
fixed and merged. thanks |
This PR allows to translate a model's name in unauthorized message using ActiveSupport I18n.