-
Notifications
You must be signed in to change notification settings - Fork 987
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
Fixes #36330 - Make translated strings from plugins available in the browser and modern frontend code #9691
Conversation
Issues: #36330 |
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've been thinking about a way to register translations during plugin registration. We have this bit:
Would it make sense to have some DSL method to register translations with a certain domain instead of having to write an initializer in every plugin?
Perhaps I'm broadening the scope here, but I've always been confused that a Rails engine registers a plugin, but in my mind it would have made more sense to make a ForemanPlugin that is an engine, or at least creates an engine.
app/views/layouts/base.html.erb
Outdated
@@ -32,6 +32,9 @@ | |||
<% end %> | |||
</script> | |||
<%= javascript_include_tag "locale/#{FastGettext.locale}/app" %> | |||
<% for plugin in ::Foreman::Plugin.all do %> | |||
<%= javascript_include_tag "locale/#{FastGettext.locale}/#{plugin.engine.class.to_s.underscore.split('/', 2).first}" %> |
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.
Does this work on foreman-tasks
? Perhaps because the domain is foreman_tasks
, but just want to make sure.
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.
foreman-tasks
is probably the sole reason why I couldn't just use plugin.name
or plugin.id
. The domain indeed has underscores in it
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.
Are you starting to understand why I'd love to rename it? Even though it's a pain once, it'll be easier in the long run.
I took a stab at this, does it somehow resemble what you had in mind? I hope I covered everything |
I think it does, very nice. Now I have more ideas so would it make sense to submit it as a standalone PR and then merge it? Like enhance the rake task to utilize the domain: foreman/lib/tasks/gettext.rake Lines 23 to 46 in 96c6d18
Then also update the plugin template to utilize the new DSL method. |
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.
Argh, I forgot to submit my review last week.
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.
And I wonder if we can either disable the spellcheck on those words or if we can add them to the known words.
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.
Looks like you can configure spellcheck/spell-checker
to ignore words in .eslintrc
. Other than that 👍 from my side, though I'm not an expert on JS. Perhaps @MariaAga can take a look at that part.
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.
Please squash the commits so we can merge them.
b8e054b
to
cc4e4f2
Compare
app/helpers/reactjs_helper.rb
Outdated
domains = ::Foreman::Plugin.all.filter_map do |plugin| | ||
domain = plugin.gettext_domain | ||
domain if (FastGettext.translation_repositories[domain]&.available_locales || []).include? locale | ||
end | ||
domains.map do |domain| | ||
javascript_include_tag("locale/#{locale}/#{domain}") | ||
end.join.html_safe |
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.
Untested, but isn't it also needed to check if domain is nil
? Or do you count on FastGettext.translation_repositories[nil
returning nil
? I was thinking about something like this:
domains = ::Foreman::Plugin.all.filter_map do |plugin| | |
domain = plugin.gettext_domain | |
domain if (FastGettext.translation_repositories[domain]&.available_locales || []).include? locale | |
end | |
domains.map do |domain| | |
javascript_include_tag("locale/#{locale}/#{domain}") | |
end.join.html_safe | |
::Foreman::Plugin.all.filter_map do |plugin| | |
domain = plugin.gettext_domain | |
if domain && (FastGettext.translation_repositories[domain]&.available_locales || []).include? locale | |
javascript_include_tag("locale/#{locale}/#{domain}") | |
end | |
end.join.html_safe |
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.
Or do you count on FastGettext.translation_repositories[nil returning nil
Yes, I did that. The assumption seems to hold, what you suggest is hands down more obvious and probably safe in the long run. It needed a small tweak (parentheses) though
828ae5b
to
11595c2
Compare
I went ahead and squashed this, unsquashed variant is still available here https://github.com/adamruzicka/foreman/tree/unsquashed/potojson |
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 will require a bit on the packaging side, but that should be ok.
bundler.d/assets.rb
Outdated
@@ -1,7 +1,8 @@ | |||
group :assets do | |||
gem 'jquery-ui-rails', '~> 6.0' | |||
gem 'patternfly-sass', '~> 3.59.4' | |||
gem 'gettext_i18n_rails_js', '~> 1.3.1' | |||
gem 'gettext_i18n_rails_js', '~> 1.4' | |||
gem 'po_to_json', '~> 1.1.0' |
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.
Any particular reason to pin to ~> 1.1.0
instead of ~> 1.1
?
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.
No, not really, changed
- Add plugin:po_to_json rake task - Load js locale files for all plugins - Make i18n.js do multi-domain lookup
const multidomain = (strict, fallback) => (...args) => { | ||
// eslint-disable-next-line no-unused-vars | ||
for (const domain of Object.keys(strictJed.options.locale_data)) { | ||
try { |
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.
Js looks fine, is the only way to do this conditional return is with try-catch?
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 haven't found any other way
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'm still wondering about storing the JSON files in git, but that's a similar problem as the MO files.
For context about the problem see https://gist.github.com/adamruzicka/f578c2e519f51230d475fd72848629d1
Concerns
webhippie/po_to_json had last change in June 2018, this change is still unreleased. Sadly, the changes proposed here rely on that unreleased change. Unless webhippie/po_to_json#8 is resolved soon-ish, we may need to either depend (and maybe package?) latest master or carry the project ourselves.Version 1.1.0 is out.webhippie/gettext_i18n_rails_js doesn't really support doing the conversion for other rails engines (and domains) than just ::Rails. The changes proposed here work around that in a rather hacky fashion. If webhippie/gettext_i18n_rails_js#57 is accepted and released soon, we could have a more sensible way of doing that, along the lines of https://github.com/theforeman/foreman/compare/develop...adamruzicka:foreman:potojson-next?expand=1
After this change is merged, all plugins will need to run the new rake task to make translations pop up.
Followup changes