Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
i18n-translators-tools
Advanced tools
This package brings you useful utility and library which can help you to handle locale files and translations in your Ruby projects. It is build upon i18n library and extends it's simple format so you can simply track field changes or keep translator's notes. Conversion back to simple format is possible and as simple as call 'i18n-translate strip'.
i18n-translate can NOT handle lambdas and procedures. The solution is to put all your rules to separate file and use exclude (repeatable) argument or create configuration file for your project in locales directory including exclude array.
in your set of keys in leaf can't be ALL keys reserved words this is ok:
en:
my:
key:
message: "some message"
reference: "http://..."
but this is NOT
en:
my:
key:
reference: "http://..."
**Reserved keywords are: **
po files are supported only partialy. If you convert from yaml or ruby to po and back you don't have to care even if you are using pluralization. If you are converting from po origin files then you can lose header of the file, pluralization, some flags (fuzzy will stay) and previous-context. Strings over multiple lines are supported, however.
po files are not compatible with I18n::Backend::Gettext. The main purpose of enabling conversions to po files is effort to allow usage of many po editors for ruby projects. You can either keep all your files in yml and convert them only for translators and then back or you can have default in yml and other locales in po files. i18-translate tool will take care of it.
QT TS format is not fully supported. TS format is changing with almost every qt release so i18n-translate don't support:
RubyGems
gem install i18n-translators-tools
Latest from sources
git clone git://github.com/pejuko/i18n-translators-tools.git
cd i18n-translators-tools
rake gem
gem install pkg/i18n-translators-tools.gem
Rails
in Gemfile
add
gem 'i18n-translators-tools'
then run
bundle install
i18n-translate brings additional metadata to locales. Therefore you have to include new backend into I18n. This new backend works as a transparent proxy and can work with both simple and enhanced format. If you are already using I18n then adding this backend will be probably the only change you have to do. I highly recommend to use this extension together with Fallback backend and all default values (usually in english) put into special file like 'default.yml'.
This will enable to have locales like en_US and en_GB where can native speakers put their polished english. I think it is good thing to put all default strings into separate file(s). If you need to change some text you don't have to touch source code.
(Default files can be now in enhanced format.)
So in your application you should do something like this:
require 'i18n-translate'
I18n::Backend::Simple.send(:include, I18n::Backend::Translate)
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.default_locale = 'default'
I18n.load_path << Dir[ File.expand_path("../locale/*.yml", __FILE__) ]
I18n.locale = 'cs'
and then you can use
I18n.t('some.key', :count => 3, :var => 'interpolated string')
as usual.
Notice that Translator have to be included BEFORE Fallbacks otherwise the fallback will get Hash (even with empty translation) and won't work.
It is hightly recommended to use Fallbacks backend together with Translate. If you have experienced nil or empty translations due to untranslated strings this can fix the problem.
Rails
Your config/application.rb
should look like this
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
I18n::Backend::Simple.send(:include, I18n::Backend::Translate)
I18n::Backend::Simple.send(:include, I18n::Backend::TS)
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
module MyApplication
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s]
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.ts').to_s]
config.i18n.default_locale = :en
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
This config file is adding support for TS files.
Simple conversion from one file format to another:
$> i18n-translate cze.yml cze.po
$> i18n-translate cze.po cze.properties
Suppose we have our locales in 'locale/' directory without sub-directories and default values are inside 'locale/default.yml' file. And we have two files with locales 'locale/de_DE.yml', 'locale/cs_CZ.yml' and 'locale/extra/cs_CZ.yml'
Merging new changes (additions, removes, field changes) to all files:
$> i18n-translate merge
locale/cs_CZ.yml...merged
locale/de_DE.yml...merged
Converting all cs_CZ files to rb format:
$> i18n-translate convert -f yml -t rb -l cs_CZ -r
locale/cs_CZ.yml...converted
locale/extra/cs_CZ.yml...converted
Show some statistics:
$> i18n-translate stat
locale/cs_CZ.yml...65% (650/1000)
locale/de_DE.yml...90% (900/1000)
PO locales and default.yml
$> i18n-translate merge
locale/cs_CZ.po...merged
locale/de_DE.po...merged
Translate more entries (built-in translator invocation):
$> i18n-translate translate -l cs_CZ
For more help run i18n-translate without parameters.
There also exists example web application. It is simple web standalone translator. You can download it at i18n-web-translator
po; supported format looks like
# there is some comment
#. extracted-comment
#: reference
#, fuzzy, changed
#| msgid Old default
msgctxt "there.is.some.key"
msgid "Default text"
msgstr "Prelozeny defaultni text"
Such po file is pretty usable with po editors.
To use po files you should include this backend istead of I18n::Backend::Gettext
I18n::Backend::Simple.send(:include, I18n::Backend::PO)
This backend can also work with natural po files. Just set your app.pot as default and then you can use it:
I18n.t("some text there")
or create alias
def _(*args); I18n.t(*args); end
_("some text there")
yml; standard yaml files in I18n simple format
rb; typical ruby files in I18n simple format
ts; QT Linguist TS format. If you are planing to do translation in qt linguist, convert to this format rather then to po. Include TS backend if you want use this foramat for locales.
I18n::Backend::Simple.send(:include, I18n::Backend::TS)
properties; support for java properties locales
I18n::Backend::Simple.send(:include, I18n::Backend::Properties)
Old format using in Simple backend is:
key: "String"
or for pluralization (depends on rules) it can be similar to this:
key:
one: "String for one"
other: "plural string"
New format looks like:
key:
old_default: "old default string"
default: "new default string"
comment: "translator's comments"
extracted_comment: "po extracted comment"
reference: "po's reference"
file: "file parsed from reference"
line: "line parsed from po reference"
translation: "translation itself"
flag: "one of (ok || incomplete || changed || untranslated || obsolete)"
fuzzy: true # exists only where flag != ok (nice to have when you want
edit files manually)
Pluralized variant should look like:
key:
one:
old_default:
default:
translation:
...
other:
old_default:
default:
translation:
...
As you can see the old format is string and the new format is hash. If you use lambdas and procs objects, you should save them in separate file(s) in different (sub)directory because i18n-translate utility can't handle them but Translate backend can.
Configuration files are normal ruby files which should return hash. It is not necessary to use all switches.
Example config file:
{
:exclude => ['rules'],
:format => 'yml',
:default => 'en_US'
:default_format => 'rb',
:verbose => true,
:locale_dir => 'locales',
:separator => '.',
:target => 'yml',
:deep => true,
:force_encoding => true,
:encoding => 'utf-8',
:quiet => false
}
FAQs
Unknown package
We found that i18n-translators-tools 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.