Extension | .json |
Smartling Identifier | json |
Example File | Example JSON with common directives |
Resources | Official JSON Standards |
Content Parsing
JSON files are based on name and value pairs. For example, in this basic JSON file, “button1” is the name and “Home” is the value.
{
"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.
Inline file directives are listed below.
Smartling normalizes non-functional spacing in translated files. Translated files will be returned with a space between the key and value in the following valid JSON format:{"key" : "value"}
Default
If you upload a JSON file without any custom Smartling 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 expression to capture a number of common placeholder formats.
- Not capture any string instructions
- Deliver translations, one file per language.
For more information, see Content Parsing.
HTML
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 directive:
string_format_paths
with the value of HTML
.
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 source and target string alignment.
Example JSON string with default JSON parsing vs HTML parsing:
"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>"
Default Parsing |
HTML Parsing |
1 string with key + variant. |
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> |
|
Plurals
ICU MessageFormat
To parse the strings using ICU MessageFormat you can use two Smartling directives (either inline or via API):
-
string_format
with a value oficu
-
string_format_paths
with a value oficu
and then specifying array of the specific keys or paths.
Smartling paths support simple wildcards. See ICU MessageFormat for additional information.
ICU MessageFormat parsing implies both placeholders and support for advanced formatting syntax such as select statements and plurals. When parsing strings as ICU MessageFormat that default placeholder syntax takes precedence and will always apply if the syntax is present.
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.
Visual Context
Visual context is not automatically provided for JSON files. To provide visual context for your JSON file, you can upload a context file manually, or integrate with Smartling’s JavaScript Library, or the Context Capture Google Chrome Extension, or an API.
An alternative method of providing visual context is to allow translators to view the raw JSON file as context. Large files can split into chunks of strings or kilobytes by your Smartling Representative.
To enable raw file image context for a project:
- Go to Settings > Contexts
- Under Use file content as visual context, switch JSON on
Preparing JSON for Translation
Directives are commands embedded in code that essentially direct Smartling to the various elements in your file. Examples include the path where your content for translation, where to find translator instructions, where any key or variants are, and command multilingual output, so all translations are on one file. See examples of these directives below.
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 node’s 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.
Keys and 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 by using the variants_enabled
directive.
Please note that keys cannot be generated for paths parsed as HTML or Markdown.
String Instructions
Smartling will automatically ingest and display any value or path paired with the "instruction"
name as instructions for translators. The instructions must precede the "translate_paths"
.
For example, for the following thread, the instruction is anything that is paired with the "translation-node" path will be captured as a file instruction for translators,
When translators translate the string, Hello, ^^USER_NAME^^
the instruction "^^username^^ will be the first name, e.g. Mary
" will be visible to them in the CAT Tool. Similarly, the string Home
will include the instruction "Used in navigation to provide link back to home page.
"
JSON
{
"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."
}
}
See File and String Instructions for more information on using instructions.
Placeholder Format
Default
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__
Specifying a custom placeholder directive at the top of the file will override the default that is set up for JSON.
Custom
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 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 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 ignored.
One of the strings has an empty/null instruction and another has no instruction at all. No errors are registered because of this.
See Placeholders in Resource Files for more on placeholders.
Exclude Content from Translation
You can specify paths to exclude from translation by using the exclude_path
directive. Note that exclude_path always must be a sub-path of path pointer in the same set of translate_paths item.
For more information on excluding content via the Smartling dashboard, see Exclude Content from Translation and Restore.
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.
Character Limits
Character limits can be set to limit the character count of the translated strings. The limit is set on the source string and applies to all languages.
Example
{
"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"
}
}
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.
See Pseudo Translations for more information on how you can view pseudo translations.
Directives
File directives are supported, both inline and via our API. Directives are specified in comments at the top of the file, in the following format:
Inline File Format
{
"smartling" : {
"directive_name" : "value",
"directive_name" : "value",
"..."
},
...
}
API Parameter
smartling.[directive_name] = [value]
Here are some examples of supported directives for JSON:
Path To Translatable Strings, Instructions, and Keys
Directive |
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:
For path and instruction, see Specifying Paths. "smartling" : {
|
Character Limits
Directive | 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 above) |
Escape Base Characters
Directive | entity_escaping |
Values | auto (default) true (or yes) false (or no) |
Description |
Controls whether base characters ( > < & " ) are "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. Exact delivery depends on file type. To use inline: |
Examples |
For example, your translation might look like this: By default, using the "auto" setting, we will assume this is HTML from the When the translated file is downloaded, the translated string will be escaped as: Using |
Unescape HTML5
Directive | entity_escaping_type |
Values |
html4 (default)|html5 (case-insensitive) |
Description |
By default, all html4 entities are unescaped, except the basic set: < > & ". When this directive is set to html5, all html5 entities will be unescaped as well. |
Examples |
|
Escape Characters the Same as Source
Directive | entity_escaping_strategy |
Values | propagate | none |
Description |
Used to retain entity escaping for all non-base entities. For example, normally we turn © into © but if we use this new directive the translation will automatically update to use escaping from the source. For each entity character, we'll check to see if it was escaped in the source and try to match (propagate) it in the target. The default is none which is the current behavior, which recognizes HTML4 entities only - if HTML5 entities are required as well, you must use the entity_escaping_type=propagate directive.
This directive can be placed inline, in the API or in a template (consult your SA about configuring directive templates). This does not affect source content at all - so using it will not result in new strings. Numerical entities are not considered at all with this directive, and are treated normally. |
Examples |
To use inline: |
Exclude Path From Translation
Directive | 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" } ] } |
Standard Placeholder Format
Directive | 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. |
Custom Placeholder Format
Directive |
placeholder_format_custom |
Values |
1) Custom Java 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). |
See Placeholders in Resource Files for more on placeholders.
Keys as Variants
Directive | key_generation_strategy | ||||||||||||||
Values | strict | partial_match | ||||||||||||||
Description |
Used to specify the method used to generate keys. This is not mandatory. The default is strict. strict = construct a variant from defined key paths - all paths must match to capture the key. |
||||||||||||||
Examples |
{ "smartling": { "translate_paths" : [{ "path" : "/strings/text", "key" : ["/strings/key1", "/strings/key2", "/strings/key3"], "key_generation_strategy" : "partial_match" }] }, "strings" : [{ "key1" : "A", "key2" : "B", "key3" : "C", "text" : "String1" }, { "key1" : "A", "key2" : "B", "text" : "String2" }, { "key2" : "B", "key3" : "C", "text" : "String3" }, { "key1" : "A", "key3" : "C", "text" : "String4" }, { "key3" : "C", "text" : "String5" }, { "text" : "String6" }, ] } }
|
Enable Variants to Make Strings Unique
Directives |
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 with the translate_paths directive to specify keys, which provides the information needed to generate variant metadata. If you have previously uploaded a file with variants turned off, and re-upload 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"} |
Control CLDR Mapping for ICU Strings
Directive | icu_plural_mappings |
Value |
on|yes|true|off|no|false The default is on. |
Description |
This directive can control CLDR mapping and allow ICU MessageFormat strings to be ingested as raw strings. By default, "=0", "=1", and "=2" automatically maps to the CLDR zero, one and two forms. If you do not want these forms to map to their CLDR counterparts, and instead retain their literal meaning, use this directive to turn off the default behavior. |
Examples |
"smartling":{"icu_plural_mappings" : "off"} |
Parse with ICU Message Format Rules
Directive | string_format |
Value | icu (default) | NONE |
Description |
Parse the strings in this JSON file using ICU MessageFormat rules for placeholders, SelectFormat, PluralFormat. |
Examples |
"smartling":{"string_format" : "icu"} |
Change how strings are parsed
Directive | 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. Wildcards are allowed in path definitions.
|
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). |
Trim Whitespace
Directive |
whitespace_trim |
Values |
on|yes|true or off|no|false or leading|trailing The default value is on |
Description |
A whitespace is any character or series of characters that represent horizontal or vertical space in typography. When rendered, a whitespace character is not a visible mark, but does occupy an area or space on a page. Although whitespaces are necessary within a string (typically to separate words), unnecessary whitespaces can be found at the start of a string (leading) and at the end of a string (trailing). With this directive, you can trim whitespaces, as it enables or disables whitespace trim management for the ingested strings. Whitespace is optionally trimmed from content then re-inserted on download for convenience so that translators do not have to manage the extra spaces. However, content owners may want to retain surrounding whitespace so that translators can By default, the leading and trailing whitespaces are trimmed. You can choose to disable trimming or specify trimming for leading or trailing whitespaces. The directive can only be used as the API request parameter. |
Examples |
Smartling will trim leading and trailing whitespaces (default)
Smartling will not trim leading or trailing whitespaces
Smartling will trim only leading whitespaces
Smartling will trim only trailing whitespaces |