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

Keys with dynamic scope arguments in views are parsed as if there were no scope argument #213

Closed
shaneog opened this issue Dec 1, 2016 · 8 comments
Labels
Milestone

Comments

@shaneog
Copy link

shaneog commented Dec 1, 2016

Firstly, sorry for opening a support issue here!

Our app uses translations using the explicitly scoped, symbols form, e.g.

I18n.t(:button_text, scope: [:search, :header_form])

We have a lot of this code in our large codebase.

We also use some dynamic keys, e.g.

I18n.t(:title_no_results, scope: [:search, params[:action]], query: params[:q])

I've tried using the following hint using i18n-tasks-use but it doesn't work using either of the below comments on the preceding line

/ i18n-tasks-use t('search.%{params[:action]}. title_no_results')
/ i18n-tasks-use t('search.%{params[:action]}. title_no_results', query: params[:q])

So right now I can't use the missing command or RSpec test because of false positives.
Am I doing something wrong or is this type of dynamic key not supported?

@glebm
Copy link
Owner

glebm commented Dec 1, 2016

I18n.t(:button_text, scope: [:search, :header_form])

This is fully supported in .rb files as long as the key and the scope argument value is a literal string/symbol or an array of literals. In the view files, there is an additional restriction that scope must be the last first argument after the key.


Dynamic keys, however, need to be listed explicitly, so e.g. if the possible actions are foo and bar, the above should be:

/ i18n-tasks-use t('search.foo.title')
/ i18n-tasks-use t('search.bar.title')

You can also put them on the same line:

/ i18n-tasks-use t('search.foo.title') t('search.bar.title')

@glebm glebm added the question label Dec 1, 2016
@shaneog
Copy link
Author

shaneog commented Dec 1, 2016

Thank you for the quick response.

In the view files, there is an additional restriction that scope must be the last argument.

Since this is used in a view, does that mean that it is not supported?

= t(:title_no_results, scope: [:search, params[:action]], query: params[:q])

I'll will use the explicit keys in the i18n-tasks-use comment, and report back.

@shaneog
Copy link
Author

shaneog commented Dec 1, 2016

Ok, I've tried using the below and it didn't work.

  / i18n-tasks-use t('search.foo.title_no_results')
  / i18n-tasks-use t('search.bar.title_no_results')
  / i18n-tasks-use t('search.foobar.title_no_results')
  / i18n-tasks-use t('search.barfoo.title_no_results')
  %h2.no-results-title= t(:title_no_results, query: params[:q], scope: [:search, params[:action]])

I get the following output when I run i18n-tasks missing:

Missing translations (1) | i18n-tasks v0.9.6
+--------+-------------------------+-------------------------------------------+
| Locale | Key                     | Value in other locales or source          |
+--------+-------------------------+-------------------------------------------+
|  all   | title_no_results_html   | app/views/search/_no_results.html.haml:10 |
+--------+-------------------------+-------------------------------------------+

Any ideas?

@glebm
Copy link
Owner

glebm commented Dec 1, 2016

The output of i18n-tasks missing looks like it is for an unrelated key, title_no_results*_html*

@glebm
Copy link
Owner

glebm commented Dec 1, 2016

Sorry, when I said the last argument, I actually meant to say the first argument after the key!

@shaneog
Copy link
Author

shaneog commented Dec 1, 2016

The output of i18n-tasks missing looks like it is for an unrelated key, title_no_results*_html*

Sorry, that was me redacting some key names. In that example it should have been title_no_results.

There are hundreds of these examples in my codebase, but I've excluded all other files except this one for now to isolate what is happening. So it's definitely this key 😕


Sorry, when I said the last argument, I actually meant to say the first argument after the key!

So it should be this?

  %h2.no-results-title= t(:title_no_results, scope: [:search, params[:action]]), query: params[:q]

I've just tried it that way too and it's still not working I'm afraid.

@glebm
Copy link
Owner

glebm commented Dec 1, 2016

I've just look at the code for this, and looks like the scope argument is completely ignored if it doesn't consist of literals only (scope_arg_re only matches literal scopes so it won't match, but it's optional (?) so the entire scope arg is ignored).

So you could submit a fix that correctly parses such scope arguments as e.g. "title_no_results.search.#{params[:action]}" or alternatively completely ignores the entire key if they have a dynamic scope argument.

Alternatively, in your codebase, you could refactor such keys to not use a scope argument (I thin.
Yet another alternative is to move such calls into .rb files, then they will be parsed correctly.

In any case i18n-tasks-use is still required.

@glebm glebm added bug and removed question labels Dec 1, 2016
@glebm glebm changed the title Dynamic Translations and i18n-tasks-use Keys with dynamic scope arguments in views are parsed as if there were no scope argument Dec 1, 2016
@glebm glebm closed this as completed in 8e6b327 Jan 19, 2017
@glebm glebm added this to the v0.9.9 milestone Jan 19, 2017
@glebm
Copy link
Owner

glebm commented Jan 19, 2017

Fix released in v0.9.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants