ilib-loctool-javascript
v1.2.0
Published
A loctool plugin that knows how to process JS files
Maintainers
Readme
ilib-loctool-javascript
ilib-loctool-javascript is a plugin for the loctool that allows it to read and localize javascript files.
Configuring the Plugin
Standard Settings
To use this plugin, you should set these two settings:
- The
projectTypesetting should be set tocustom - The
resourceFileTypessetting should be set to an object that includes thejavascriptproperty. The value names the plugin that will be used as a resource file format.
Custom Settings
The plugin will look for the javascript property within the settings
of your project.json file. The following settings are
used within the json property:
- wrapper: specify a regular expression that matches the wrapper function that contains strings to extract and unique ids
- outputSourceLocale: when
true, write out a resource file for the source locale using the extracted source strings. This is useful when strings are extracted from source code and the source locale resource file must exist at runtime. The output is sent to whatever resource file type is configured inresourceFileTypes.javascript, such as this plugin or the json plugin. Default isfalse. - mappings: a mapping between file matchers and an object that gives
info used to localize the files that match it. This allows different
json files within the project to be processed with different schema.
The matchers are
a micromatch-style string,
similar to the the
includesandexcludessection of aproject.jsonfile. The value of that mapping is an object that can contain the following properties:- schema: schema to use with that matcher. The schema is
specified using the
$idof one of the schemas loaded in theschemasproperty above. The default schema is "strings-schema" which is given in the previous section. - method: one of "copy" or "sparse"
- copy: make a copy of the source file and localize the string contents. (This is the default method if not specified explicitly.)
- sparse: make a copy of the source file but only include localized strings
- template: a path template to use to generate the path to
the translated
output files. The template replaces strings in square brackets
with special values, and keeps any characters intact that are
not in square brackets. The default template, if not specified is
"resources/[localeDir]/[filename]". The plugin recognizes
and replaces the following strings in template strings:
- [dir] the original directory where the matched source file came from. This is given as a directory that is relative to the root of the project. eg. "foo/bar/strings.json" -> "foo/bar"
- [filename] the file name of the matching file. eg. "foo/bar/strings.json" -> "strings.json"
- [basename] the basename of the matching file without any extension eg. "foo/bar/strings.json" -> "strings"
- [extension] the extension part of the file name of the source file. etc. "foo/bar/strings.json" -> "json"
- [locale] the full BCP-47 locale specification for the target locale eg. "zh-Hans-CN" -> "zh-Hans-CN"
- [language] the language portion of the full locale eg. "zh-Hans-CN" -> "zh"
- [script] the script portion of the full locale eg. "zh-Hans-CN" -> "Hans"
- [region] the region portion of the full locale eg. "zh-Hans-CN" -> "CN"
- [localeDir] the full locale where each portion of the locale is a directory in this order: [langage], [script], [region]. eg, "zh-Hans-CN" -> "zh/Hans/CN", but "en" -> "en".
- [localeUnder] the full BCP-47 locale specification, but using underscores to separate the locale parts instead of dashes. eg. "zh-Hans-CN" -> "zh_Hans_CN"
- schema: schema to use with that matcher. The schema is
specified using the
Example configuration:
{
"settings": {
"json": {
"wrapper": "rb\\s*\\.getString(JS)?",
"schemas": "./json/schemas",
"mappings": {
"**/appinfo.json": {
"schema": "http://www.lge.com/json/appinfo",
"method": "sparse",
"template": "resources/[localeDir]/appinfo.json"
},
"src/**/strings.json": {
"schema": "http://www.lge.com/json/strings",
"method": "copy",
"template": "[dir]/strings.[locale].json"
}
}
}
}
}In the above example, any file named appinfo.json will be parsed with the
http://www.lge.com/json/appinfo schema. Because the method is sparse,
only the parts of the json file that have translated strings in them will
appear in the output. The output file name is sent to the resources directory.
In the second part of the example, any strings.json file that appears in
the src directory will be parsed with the schema http://www.lge.com/json/strings
and a full copy of the file, including the parts that were not localized,
will be sent to the same directory as the source file. However, the
localized file name will also contain the name of the locale to distinguish
it from the source file.
If the name of the localized file that the template produces is the same as the source file name, this plugin will throw an exception, the file will not be localized, and the loctool will continue on to the next file.
License
Copyright © 2019, 2022, 2026 JEDLSoft
This package is released under the Apache License, Version 2.0. The full license text is available in the LICENSE file in the ilib-mono repository on GitHub.
Release Notes
See CHANGELOG.md.
