Soluling home   Document home

Composite format strings and interpolated strings

Composite format strings and interpolated strings

Most applications use composite format strings or interpolated strings. They are used when making it possible to create strings that are dynamic. This means that you don't know the value used in the string on compile-time, but the actual value depends on the parameters that appear on run time. The classic version of the composite string is C's printf function. The basic idea of the function is to have a pattern that contains one or more placeholders for dynamic variables. The function takes the pattern and the variable amount of parameters and injects the parameter values into the placeholders of the pattern resulting a single string where pattern and parameters are merged. For example

printf("Hello %s", name);

If the name is "John" we will get "Hello John".

We can have any number of parameters as long as the placeholder count in the pattern matches the parameter count.

printf("Hello %s and %s", name1, name2); 

If name1 is "John" and name2 is "Jill" we will get "Hello John and Jill".

Composite format strings are also used with plurals and genders.

Each platform or programming language uses a different format for patterns. The following table shows the most common pattern types and the patterns that Soluling can handle:

Platform Sample composite string
.NET Hello {0}, welcome to {1}
Angular Hello {{name}}, welcome to {{place}}
C/C++

Hello %s, welcome to %s

Delphi

Hello %s, welcome to %s

Java

Hello %s, welcome to %s
Hello {0}, welcome to {1}

Objective C

Hello %s, welcome to %s

Python

Hello {0:s}, welcome to {1:s}

WiX

Hello [0], welcome to [1]

As you can see, there are several formats, and therefore Soluling when scanning, converts the platform-specific format into a platform-independent format. No matter what is the source of the composite string, the string appears in the same way in Soluling. When building Soluling converts the platform-independent format back to the platform-specific format.

Translators do not need to know the native format syntax of each platform. The grid shows placeholders visually such a way that the placeholder is a single item. The translator can move the placeholder within the string, but the editor makes sure that the translation has the same placeholders as in the original. If you want to give translator a bit more information turn on the extended placeholder scanning (see the scan modes later on this page).

Some platforms, such as Angular contain the variable name or expression inside the placeholder. In that case, Soluling shows the variable name or expression instead of the index of the placeholder.

If such as string has two or more named placeholders, Soluling also includes the index part.

This is because some platforms let you use an expression in the placeholder instead of a single variable. If you have an expression, it is more likely to be changed than just a variable name. Adding an index makes it possible to locate the right placeholder even in the case the expression has been changed.

Note! When working with right-to-left languages such as Arabic or Hebrew, the editor shows placeholders as {0}, {1}, etc.

Composite format string scan modes

By default, Soluling scans the composite format strings in the way described above. However there are two other methods. Use the project's options dialog to change it. Possible values are:

Value Description Example
Compact Default. If checked Soluling scans composite format strings as an universal form that means the placeholders show as a platform independed format. Each placeholder is marked with a yellow box with the placeholder number. Compact placeholder
Extended If checked Soluling works as above but in addition of the placeholder number, the placeholders show the formatting specific items. Extended placeholder
Plain text If checked Soluling scans composite format strings as a plain text and does not convert them to the universal form.. Plain placeholder

Interpolated strings

Recently, strings interpolation has been widely used instead of printf-like traditional composite strings. When using dynamic languages such as JavaScript, interpolated strings can be externalized into a resource file and then localized. However, when using compiled language such as C#, the externalization cannot be done. This is because, under the hood, the string interpolation is a just series of string concatenations. If you use a compiled language and you want to localize a dynamic string, use the composite string function, not the string interpolation.

Comment checker

Resource strings, in general, are harder to translate compared to UI strings. This is because there is no visual context, but the translator needs to figure out the meaning just by reading the value of the string and if existing by reading the comment. Composite strings are even hard to translate because parts of the complete string are missing (i.e., placeholders). This is why it is very important to fill the gaps but to provide a comment that explains the pattern and each placeholder in the pattern. The most convenient way to add comments is to add them to the source. Most platforms provide a way to add comments into resource strings. Use them. It is also quite easy to forget to add a comment. This is why Soluling contains a composite comment checker. It works when scanning and writes information about missing comments into the log files. The checker helps tp detect the resource strings that have no comments.