JSON
Extension | .json |
Smartling Identifier | json |
Example File | (see example below) |
Resources | Official JSON Standards |
JSON files are based on name/value pairs. For example, in this basic JSON file “button1” is the name and “Home” is the value.
JSON{
"button1": "Home"
}
Name/value pairs can be nested so that a path of several names leads to a value.
Smartling parses JSON files for translatable strings, and for key/variant metadata (Smartling keys) and translator instructions relating to those strings. Only JSON values can become strings or translator instructions in Smartling. Depending on how your file is structured, however, you may want to capture either a JSON value, or part of a name path as a key. Smartling allows for either option using the translate_paths
directive.
Default Parsing
If you upload a JSON file without any custom Smartling Integration directives Smartling will:
- Capture all values in the file as strings.
- Use the complete name path to each string as key and variant data.
- Create placeholders in the strings using a default regular express to capture a number of common placeholder formats.
- No Smartling String Instructions are captured.
- Deliver translations, one file per language.
To parse the strings using ICU MessageFormat you must use the Smartling file directive string_format (either inline or via API). See ICU MessageFormat for more info. If ICU string formatting is used, then that takes precedence over HTML and any HTML directive will be ignored if your directives resolve in a value being parsed for both ICU and HTML. It is possible in a single file to have both HTML formatted strings and ICU formatted strings but they must be on different paths.
If a string contains HTML, Smartling automatically parses the strings in the file with basic HTML parsing, even without any HTML directive. Just note, this basic parsing will not break down the JSON value with HTML into smaller strings. It is ingested as one string. If this is not desirable and you want the value to be broken down into smaller strings in Smartling based on the HTML formatting, you can use the string_format_paths HTML feature. This will benefit both your translation resources and your translation memory leverage.
It is worth noting that in doing so, you lose any "keys", however, you still have "variants". In most cases, such as translation Jobs, this is acceptable, however, this is not acceptable if you are importing translations from a file, as keys are critical for alignment.
Example JSON value:
"terms": "<p>These are the terms you should be aware of them.</p><h1>First things first</h1><p>The first thing about the terms is that they apply to you.</p><h1>Second things second</h1><p>The second things about the terms is that they apply to us.</p>"
Regular Parsing (1 string with key + variant) |
HTML parsing (5 strings with variant only - translations cannot be imported) |
<p>These are the terms you should be aware of them.</p><h1>First things first</h1><p>The first thing about the terms is that they apply to you.</p><h1>Second things second</h1><p>The second things about the terms is that they apply to us.</p> |
|
Default Placeholder Format
If you do not specify a custom placeholder format, even if you specify other directives, Smartling will convert the following subtext of a string into a placeholder for that string:
- {x}
- {{x}}
- ${x}
- %x%
- %%x%%
- ##x##
- __x__
Custom Integration
If you want to control how Smartling will capture the content and metadata from your JSON file, or specify a custom placeholder format, you can use custom Smartling directives. The directives can be added "inline" to the file or they be configured as API parameters.
JSON inline directives must be the "first" or "top" object in the JSON
If you are integrating "inline" the the integration "smartling" must be the first object in the JSON.
The syntax for directives might change if you are using them as API parameters instead of inlining them.
Below is an example of a JSON document with custom directives integrated in the file:
json.json file example:
{
"smartling" : {
"translate_paths": [{
"path": "*/translation",
"key": "{*}/translation",
"instruction":"*/translation-note"
}],
"placeholder_format_custom": ["\\^\\^.*?\\^\\^"],
"variants_enabled" : "true"
},
"Key1": {
"attribute": "4215050",
"translation": "Hello, ^^USER_NAME^^.",
"translation-note": "^^username^^ will be the first name, e.g. Mary"
},
"Key2": {
"attribute": "400913",
"translation": "Home",
"translation-note": "Used in navigation to provide link back to home page."
},
"Key3": {
"attribute": "4215049",
"translation": "Copyright 2017",
"translation-note": ""
},
"Key4": {
"attribute": "4213290",
"translation": "About Us"
}
}
The value of placeholder_format_custom is a regular expression (regex). We want to match on a "^" character as part of our expression. "^" is a reserved character for regex so we must escape it with "\". Because we are saving this in a JSON file where "\" is itself a reserved character for escaping, we must escape it for the JSON file with an additional "\".
Uploading this file will result in the following strings and metadata:
String | Key | Instruction |
Hello, ^^USER_NAME^^. | Key1 | ^^username^^ will be the first name, e.g. Mary |
Home | Key2 | Used in navigation to provide link back to home page. |
Copyright 2017 | Key3 | |
About Us | Key4 |
The values of the "attribute" names are completely ignored.
One of the strings has an empty/null instruction and another has no instruction at all. No errors are registered because of this.
Keys/Variants
By default, the full path of keys leading to a string are captured as Key and Variant metadata. Custom key paths can be set and variant behavior turned off with directives
Specifying Paths
Some directives require you to specify a path or set of paths to keys or strings in the file. A path is a slash-separated string which uses Xpath-like syntax (although it’s not Xpath.). The nodes separator is always / (slash).
Wildcards are allowed in path definitions.
- If no wildcards, then the path identifies a single node and all its children (“exact subpath”).
- If wildcards, then a path identifies a set of nodes and all their children (“path pattern”). Currently, only the * (asterisk) wildcard is implemented, which means “one or more nodes with any names”.
- Exact subpaths have precedence over path patterns.
For example, in the following path specifications:
[*/text, */string, system/log/text, system/log/text/details]
Smartling processes these paths as follows:
- /description/text — matches */text.
- /description/general/text - matches */text directly.
- /description/text/general — matches its parent, /description/text , matches */text.
- /system/log/text — matches both /system/log/text (exact path) and */text (pattern). Exact path takes precedence.
- /system/log/text/details — matches its parent, /system/log/text and system/log/text/details (exact). Exact path takes precedence.
See Placeholders in Resource Files for more on placeholders.
Return Untranslated Strings as Empty
When using the API file/get to download JSON files from Smartling, the parameter includeOriginalStrings=false ensures that if no translations are available, Smartling returns an empty string. If the parameter is set to true, Smartling returns the original string.
Pseudo Translations
The pseudo_inflation directive is not supported as an inline directive for JSON files, however, you can still control pseudo inflation for JSON files by including it as an argument in the API upload request. For example: smartling.pseudo_inflation=70 will result in pseudo translation length being increased by 70 percent.
Directives
Format
JSON{
"smartling" : {
"directive_name" : "value",
"directive_name" : "value",
"..."
},
...
}
character_limit
Values | integer or NONE |
Description | Sets or removes the maximum characters allowed for a translation. If NONE is specified, character limits will be removed. (You can also remove the limit key/value pair and re-upload a file.) |
Examples | (See example below.) |
{
"smartling" : {
"translate_paths" : [
{
"path":"/*/text",
"key":"/*/id",
"character_limit" : "/*/limit"
},
{
"path":"/*/value",
"key":"/*/key",
"character_limit" : "/*/charlimit"
}],
},
"testkey1" : {
"limit" : 10,
"text" : "item1",
"id" : 1
},
"testkey2" : {
"limit" : 20,
"text" : "item2",
"id" : 2
},
"testkey3" : {
"limit" : 30,
"text" : "item3",
"id" : 3
},
"testkey4" : {
"charimit" : 100,
"value" : "item4",
"key : "key"
}
}
entity_escaping
Control whether or not certain characters will be HTML escaped when delivering the translations.
Values | auto (default) true (or yes) false (or no) |
Description | Can control whether or not characters will be "escaped" into entities when delivering translations. This can be set universally for the whole file via API, or by setting the directive at the top/start of the file. The directive can also be placed inline to control the behavior of specific strings. In Auto mode Smartling escaping will depend on whether Smartling believes the specific string requires HTML escaping. With true or false this will force the behavior for all strings. |
Examples |
To use inline: "smartling":{"entity_escaping": "false"} |
For example, your translation in the dashboard might look like this:Smartling <hr> HTML escaping <, >, &, " example
By default, using the auto setting, we would assume this is HTML from the <hr> tag present in the translation and it would be delivered as:
Smartling <hr> HTML escaping <, >, &, ", example
When downloading the translated file.
Using false would deliver:
Smartling <hr> HTML escaping <, >, &, ", example
with no characters entity escaped.
exclude_path
Values | The value of this directive is expressed as exclude_paths:[path]. |
Description |
Excludes the specified path as content that will be translated. Content will not be parsed. Notice that exclude_path always must be a sub-path of path pointer in the same set of translate_paths item. |
Examples |
{ "smartling" : { "translate_paths" : [ { "path":"/nodes/*", "exclude_path":"*/comment" }, { "path":"/messages/*", "exclude_path":"*/note" } ] }, "nodes" : [ { "text" : "item1", "comment" : "comment1" }, { "text" : "item2", "comment" : "comment2" }, { "text" : "item3", "comment" : "comment3" } ], "messages" : [ { "message" : "message1", "note" : "note1" }, { "message" : "message2", "note" : "note2" }, { "message" : "message3", "note" : "note3" } ] } |
placeholder_format
Values | NONE; C; IOS; PYTHON; JAVA; YAML; QT, RESX |
Description | Used to specify a standard placeholder format |
Examples |
"smartling":{"placeholder_format" : "IOS"} Specifies iOS-style placeholders for the file. |
placeholder_format_custom
Values |
1) Custom Perl compatible regular expression. |
Description | Specifies a custom placeholder format. Any text in your file matching the regular expression you provide will be captured as a placeholder. |
Examples |
"smartling" : { When customizing placeholders with this directive inside the file, the value must be expressed as an array of strings (square brackets). |
translate_paths
Description |
The translate_paths directive tells Smartling which parts of your JSON file are translatable strings, keys and instructions. The value is an array of objects. Each object contains one path, that is at the minimum the path to the strings to extract. For each path you can also optionally specify how to assign the key and translator instructions for each string found on that path. The object takes three parameters: path - the name path leading to a translatable string (required) "smartling": {
|
translate_paths (to be deprecated)
Values | A comma-separated list of paths to be captured as strings for translation. Note: for new files, use the new translate paths object. |
Description | When included in this list, all plain text within the specified path will be considered a translatable string. |
Examples |
"translate_paths": ["*/translation"] Smartling will tranlate content in any path ending in translation, such as string/translation nav/translation, etc. |
source_key_paths (deprecated)
Values |
A comma separated list of paths to use create “keys” for strings on translate_paths. The key will be a space separated string of all the keys leading to the source string. For example: “string”, “group1 string”. For new files, use the new translate paths directive. |
Description |
Used to define the schema for capturing a key for each source string. Keys are required: If you want to import pre-existing translations from a file with the same structure Specify the full path to the value, then indicate which part of the path should be used as the key using {} notation. |
Examples |
"source_key_paths" : "{*}" Smartling will capture the full path of keys leading to each translatable string as Key metadata. "source_key_paths" : "data/item/{string_name/*}" Smartling will capture string_name [any subsequent keys] as the key’ |
variants_enabled
Values |
true|TRUE|on|ON OR false|FALSE|off|OFF |
Description |
When enabled, Smartling will make strings unique using variant metadata. Must be used in conjunction with the source_key_paths directive, which provides the information needed to generate variant metadata. If you have previously uploaded a file with variants turned off, and reupload the file with variants on, Smartling will capture all content as new strings. You can configure Smartmatch to automatically match the existing translations. |
Examples |
"smartling": {"variants_enabled" : "TRUE"} |
string_format
Value | icu |
Description |
Parse the strings in this JSON file using ICU MessageFormat rules for placeholders, SelectFormat, PluralFormat. |
Examples |
"smartling":{"string_format" : "icu"} |
string_format_paths
Values | The value of this directive is expressed as [format]:[paths]. |
Description |
Specifies the format of strings for the specified paths and can enable HTML inside another file format. Currently supported formats are:
Separate multiple paths by commas. You may specify a single path for a format or a comma-separated list of paths enclosed in square brackets. The list may be empty. |
Examples |
"smartling": {"string_format_paths" : "html: *"}
Smartling parses values of all nodes as HTML. ---- "smartling": {"string_format_paths" : "html: */text"}
Smartling enables HTML in text nodes (and their subnodes), regardless of their parents. ---- "smartling": {"string_format_paths" : "html: [/text, string]"}
Smartling enables HTML in text and string nodes (and their subnodes), regardless of their parents. ---- "smartling": {"string_format_paths" : "html: /product/description"}
Smartling enables HTML in /product/description and subnodes. ---- "smartling": {"string_format_paths": "icu: [*]"}
Smartling parses all strings as ICU MessageFormat ---- "smartling": {"string_format_paths" : "html: */text, @default: /system/log/text"}
Smartling enables HTML in text nodes (and subnodes), but disables HTML in /system/log/text (and subnodes), as the exact match overrides the pattern match. |
Considerations |
When to use multiple string_format_paths in a single document, recommended use case: If your document has disparate kinds of strings that are used in different contexts, for example, if your single JSON file contains application UI strings, with standard string formatting and placeholders, together with one or few keys that are just giant HTML documents (example; TOS or Privacy Policy). |