Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
i18next is a library facilitating internationalization and localization (i18n) of your Python applications
The idea of the project came from the experiences we encountered during the internationalization of projects created as part of lemon.markets. We were not entirely satisfied with the i18n tools available on the market. It was mainly about the simplicity of generating translation files, their format, the ease of generating a translation based on a given context or integration with tools and processes supported by external companies such as localise or phrase. We figured out we would write our own module that would improve the life of a Python developer in this area.
Please note that this project is in the early development phase. We would really appreciate if you could help us to improve it. Fell free to report any issue or suggest a new feature.
You can install library using pip:
pip install i18next
The table below describes the terms used in this documentation.
Term | Description |
---|---|
translation key | unique key identifying translation string |
translation string | string to be translated |
translation context | data used to interpolate translation string |
translation file | file containing mapping of translation keys and translation strings |
translation | interpolated translation string |
i18next
assumes the existence of the locale directory containing translation files. The locale directory path can be
configured (see section Configuration).
Translation files are JSON files containing mapping between translation keys and translation strings.
The translation file should be named as <language>.json
where<language>
is a language code.
An example of translation file content is presented below:
{
"translation-key-1": "This is a text without interpolation",
"translation-key-2": "This is a text with interpolation: { interpolated-value }"
}
An example directory structure of your locale directory should be similar to:
locale/
en.json
fr.json
es.json
pt.json
...
i18next
configuration is being done by modifying global config
object from i18next
package.
It should be done before first usage of trans
function:
from i18next import config
config.fallback_lang = 'en'
config.locale_path = '/path/to/your/locale/directory'
config.strict = False
The table below describes the configuration parameters:
Parameter | Description | Default value |
---|---|---|
fallback_lang | Fallback language to search for translation key if one cannot be found for requested language | en |
locale_path | Locale directory path | ./locales |
strict | Flag determining the behavior of trans function. If set to False - key itself will be returned in case one has not been found or interpolation has failed , otherwise - exception will be raised | False |
trans
function is used to get translation based on translation key and requested language.
from i18next import trans, config
# './locale/en.json':
# {
# "key-1": "Welcome {firstname} {lastname}!",
# "key-2": "Good morning!",
# "key-3": "Bye!"
# }
# './locale/pl.json':
# {
# "key-1": "Witaj {firstname} {lastname}!",
# "key-2": "Dzień dobry!"
# }
config.fallback_lang = 'en'
config.strict = False
en_trans1 = trans('key-1', params={'firstname': 'John', 'lastname': 'Doe'}) # 'Welcome John Doe!'
en_trans2 = trans('key-2') # 'Good morning!'
en_trans3 = trans('key-3') # 'Bye!'
pl_trans1 = trans('key-1', params={'firstname': 'John', 'lastname': 'Doe'}, lang='pl') # 'Witaj John Doe!'
pl_trans2 = trans('key-2', lang='pl') # 'Dzień dobry!'
pl_trans3 = trans('key-3', lang='pl') # 'Bye!' - fallback case because of missing 'key-3' in pl translation
# config.strict=False so key itself is returned when translation key or file is missing
missing_en_trans = trans('missing-key1') # 'missing-key1'
missing_pl_trans = trans('missing-key2', lang='pl') # 'missing-key2'
missing_fr_trans = trans('missing-key3', lang='fr') # 'missing-key3' because translation file is missing
trans
function is raising exceptions in strict mode (config.strict=True
) when:
from i18next import trans, config, errors
# './locale/en.json':
# {
# "key-1": "Welcome {firstname} {lastname}!",
# "key-2": "Good morning!"
# }
config.strict = True
en_trans1 = trans('key-1', params={'firstname': 'John', 'lastname': 'Doe'}) # 'Welcome John Doe!'
en_trans2 = trans('key-2') # 'Good morning!'
# interpolation error
try:
trans('key-1', params={'firstname': 'John'}) # missing lastname
except errors.TranslationFormatError:
...
# missing translation key
try:
trans('missing-key')
except errors.TranslationNotFoundError:
...
# missing translation file
try:
trans('key-2', lang='pl')
except errors.TranslationFileNotFoundError:
...
Error hierarchy available in strict mode is presented below:
I18nError
- base class for all exceptions thrown within i18next
library
TranslationFileNotFoundError
- missing translation file errorTranslationNotFoundError
- missing translation key errorTranslationFormatError
- interpolation errori18next
library provides a command line interface to extract translation keys from Python source code.
bash$ i18next --help
usage: i18next [-h] [--locale [LOCALE]] [--lang [LANG]] [--debug] [search_paths ...]
positional arguments:
search_paths Paths to search for python files
options:
-h, --help show this help message and exit
--locale [LOCALE] Locale directory path
--lang [LANG] Language code
--debug, -d Enable debug logging
Basic usage is presented below:
i18next --locale ./locale --lang en --search_paths ./your_package/ ./your/module.py
i18next
CLI:
trans
function callsTranslation file to be updated is selected based on language provided in cli call (--lang
).
Please note that currently i18next
is not removing any translation keys from translation file.
As mentioned above, i18next
CLI is detecting translation keys by analysing trans
function calls.
Currently, we support following statements:
#1
from i18next import trans
trans("1")
#2
from i18next import trans as _
_("2")
#3
import i18next
i18next.trans("3")
#4
import i18next as _
_.trans("4")
#5
class A:
def a(self):
from i18next import trans
trans("1")
def b(self):
from i18next import trans as _
_("2")
def c(self):
import i18next
i18next.trans("3")
def d(self):
import i18next as _
_.trans("4")
FAQs
i18next - library facilitating internationalization and localization (i18n)
We found that i18next demonstrated a healthy version release cadence and project activity because the last version was released less than 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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.