Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Gem to visualize i18n strings within anay rails/ruby project using the commonly used i18n gem.
Problem:
It is very difficult for non-developers (e.g. translators and product managers) working with i18n Rails apps to make the connection from a string within the app to the correct i18n key to edit the corresponding translation. "I would like to tweak the wording of the subtitle on this page, what string do I need to edit?"
Solution:
My solution is to hack the i18n keys into the frontend, so that the keys can be displayed as nice overlay tooltips on top of their strings right within the app.
If you you use a translation management service, such as http://webtranslateit.com, https://lingohub.com, http://localeapp.com/, you get the additional benefit of clickable links that point you right to the correct string to edit within your translation tool.
1.0.0 was basically a whole new approach how i18n hooks into your application, so if you're using a version below 1.0, this is for you:
If you required the i18n_viz javascript somewhere, remove it :)
// = require i18n_viz
If you required the i18n_viz stylesheet somewhere, remove it :)
/* = require i18n_viz.css */
Remove the initializer from config/i18n_viz.rb
However, with just a little bit of manual work, you should be able to get the whole thing to run without Rails and CoffeeScript. Just let me know if you need help.
Add the following line to your Gemfile
and run bundle install
:
gem 'i18n_viz'
add do e.g. config/application.rb
:
config.middleware.use(I18nViz::Middleware)
if you want to configure an external tool
config.middleware.use(I18nViz::Middleware) do |viz|
viz.external_tool_url = "https://webtranslateit.com/en/projects/xxx/locales/en..de/strings?utf8=✓&s="
end
if you want to configure CSS overrides
config.middleware.use(I18nViz::Middleware) do |viz|
viz.css_override = "/* valid CSS text here */"
end
Add the i18n_viz=true
parameter to visualize the translatable segments.
Alternatively, a cookie with the name i18n_viz
can be set to any value to achieve the same effect.
The gem works by overwriting the t()
and translate()
helpers in your rails app to add the key of the i18n string after the actual translated content:
en:
examples:
my_string: "My internationalized string"
foo: "bar"
%span= "#{t("examples.my_string")} : #{t("examples.foo")}"
Will result in
<span>My internationalized string--examples.my_string-- : bar--examples.foo--</span>
The i18n_viz Javascript then parses this and enriches it into:
<span class="i18n-viz" data-i18nKeys="['examples.my_string', 'examples.foo']">My internationalized string : bar</span>
The so enriched elements then get nice little tooltips attached with the i18n keys and possibly links to where they can be found/changed.
The keys will currently only work for strings that are translated in the view layer using the translate()
and t()
i18n view helpers. If you translate a string the model layer using I18n.translate
method directly (e.g. in ActiveRecord validations) the keys are not displayed in the frontend.
For certain special strings within your app, the added --key--
could potentially break your custom logic. For those cases you need to call the t()
method the additional i18n_viz: false
parameter.
Example cases include:
Iterating overy keys
de:
types:
foo: "Foo"
bar: "Bar"
- t("types").each do |key, string|
.. do something wit the key ..
JS data-attributes
de:
date:
js_format: "dd.mm.yyyy"
%body{:"data-date-format" => t("date.js_format")}
Will result in this broken output:
<body data-date-format="dd.mm.yyyy--date.js_format--">
Big thanks to my employer Railslove for supporting my open source work and to everybody who helped me.
This project is under MIT-LICENSE.
FAQs
Unknown package
We found that i18n_viz demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.