Apple Strings file (.strings)

 

Apple Strings (.strings) is a text-based file format that is widely used in Apple's development ecosystem for storing localization data. It is built around a simple structure that consists of key-value pairs, where each key is a string identifier, and the corresponding value is the actual text or translation. Both the key and value are strings of text enclosed in double quotation marks, and are separated by an equals sign. The end of each key-value pair is marked by a semicolon.

Apple Strings file example

"GeneralLearnMore" = "Learn more";

/* Common usage of the "Next" text. */
"GeneralNext" = "Next";

/* Common usage of the "Previous" text. */
"GeneralPrevious" = "Previous";

/* Common usage of the "Welcome <NAME>!" text. 
At runtime, the placeholder is replaced with a concrete value.
Example: "Welcome John!" */
"GeneralWelcome" = "Welcome %s!";

/* Intro message split into two lines. */
"PageHomeIntro" = "Welcome to our website,\nwhere technology and innovation meet creativity and design.";

/* Welcome message that includes the user's first and last name. */
"PageHomeSubtitle" = "Welcome %1$@ %2$@!";

/* Home page title */
"PageHomeTitle" = "Home";

Format Specifier

Format specifiers are placeholders used to insert values into text dynamically. They are indicated by a percentage sign (%) followed by a character that indicates the type of value to be inserted (e.g., %s, %d, %f, etc.). These placeholders work like formatting characters in printf statements, which are commonly used in programming languages like C to format output.

Positional formatting specifiers allow you to specify the order in which variables are inserted into a string. They are denoted by a numbered index in the syntax, such as %1$s, %2$d, and %3$f. These specifiers are particularly useful in messages with multiple formatting specifiers, where the order of the specifiers might change during translation into different languages. By specifying the position of each variable, you can be sure that the correct variable is inserted in the correct place in the translated string.

In addition to specifying the order of variables in a string, you can also apply additional formatting options to format specifiers in Apple Strings file format. For example, you can use the “%.2f” format specifier to indicate that a floating-point number should be formatted with two decimal places.

Here are some examples of formatting specifiers used in localization messages:

"UserWelcome" = "Welcome, %s!";

"UserNotification" = "The %1$s has assigned you a %2$s task.";

"FormattedMathPi" = "Pi: %.2f";

A brief overview of the format specifiers:

Format SpecifierDescription
%@Objective-C object, printed as the string
%%'%' character
%d, %DSigned 32-bit integer (int)
%u, %UUnsigned 32-bit integer (unsigned int)
%xUnsigned 32-bit integer (unsigned int), printed in hexadecimal lowercase
%XUnsigned 32-bit integer (unsigned int), printed in hexadecimal uppercase
%o, %OUnsigned 32-bit integer (unsigned int), printed in octal
%f64-bit floating-point number (double)
%e64-bit floating-point number (double), printed in scientific notation lowercase e
%E64-bit floating-point number (double), printed in scientific notation uppercase E
%g64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%G64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%c8-bit unsigned character (unsigned char)
%C16-bit UTF-16 code unit (unichar)
%sNull-terminated array of 8-bit unsigned characters
%SNull-terminated array of 16-bit UTF-16 code units
%pVoid pointer (void *), printed in hexadecimal lowercase with a leading 0x
%a64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent
%A64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using an uppercase P to introduce the exponent
%F64-bit floating-point number (double), printed in decimal notation

Comment

Comments are used to provide additional information or context to translators or other developers who may be working with the file. They can be used to explain the purpose of a particular string, provide usage examples, or even to leave notes for future reference. Comments in .strings files are marked with the // or /**/ syntax.

// This is a single-line comment

/* This is also a comment */

/* This is a 
multi-line comment.*/

Newline

Below are some examples of newlines in Apple Strings files.

"WelcomeMessage" = "Welcome!\nGlad to see you again!";

"WelcomeMessageExtended" = "Welcome!
Glad to see you again!
Hope you like our new design!
";

Which characters should be escaped in Apple Strings files?

Certain characters need to be preceded by a backslash before they can be included in a string in Apple Strings file format. These characters include double quotation marks ("), backslash character (\), and special control characters such as linefeed (\n) and carriage returns (\r).

What are the most common mistakes when working with Apple Strings files?

The most common mistakes when working with Apple Strings files include not properly escaping characters, using the wrong encoding, not using placeholders correctly, and not translating all strings.

How to handle plural in Apple Strings files?

Pluralization is not supported in Apple Strings files. To handle plurals correctly, you should use a strings dictionary (.stringsdict) file format.

Tired of manually editing translation files?

Our platform streamlines software localization for you.

Copyrights 2024 © Localizely