Automatically translate .string files with “Translate Files with AI”

What is a .strings file?

A .strings file is a text file format used in software development for localizing texts on Apple platforms such as macOS, iOS, and watchOS. It contains key/value pairs to store strings for different languages that are displayed in the application. This facilitates localization and translation of the application into different languages.

Here are two simple examples of a .strings file:

/* English */
"welcome_message" = "Welcome to our app!";
"login_button" = "Log In";
/* German */
"welcome_message" = "Willkommen in unserer App!";
"login_button" = "Anmelden";

The examples show a .strings file with strings for two languages: English and German.

In the first example, the keys “welcome_message” and “login_button” are defined. For the English language, the values of the keys are “Welcome to our app!” and “Log In”.

In the second example, the same keys are defined for the German language. The values of the keys here are “Willkommen in unserer App!” and “Anmelden”.

If the app is running on a device with English as the system language, it will use the English strings. However, if it runs on a device with German as the system language, it uses the German strings. In this way, the application can be localized and customized for different countries.

The task

If a source language is to be translated into several target languages, the work becomes very time-consuming. Experience shows that if you do it manually, many individual steps are necessary, which take a lot of time. This is where the Translate Files with AI app comes into play, which is available for macOS and Windows.

Translate Files with AI is a program for automated translation of text in text files into one or more languages. This software uses DeepL to translate the texts. DeepL is currently the world’s best translation service and provides meaningful, understandable translations.

The procedure

Definition of the text to be translated

In Translate Files with AI there are 5 input fields where regular expressions describing the text to be translated can be entered:

  1. Section title and Section end
    describe the format of a message or text in the file. In the above example, the section heading corresponds to the keys "welcome_message" = and "login_button" = and the section end corresponds to the string ";.
  2. Sections to ignore and Sections to translate
    describe the sections or keys whose value or content is to be handled accordingly.
  3. Strings and characters to ignore
    Here you specify which parts of the content of individual sections are to be excluded from the translation.

To translate the text in a .strings file, I use the following values in each input field:

Input fieldContentDescription
Section title[^\n+][\S ]+= "The text of the key in the format
The text of the key in the format
One or more line feeds followed by the text of the key, ending with the characters =_"_

(equal sign, space, quote, space)
Section end";(.//.)*\nThe end of the key values in the format
Quotation marks, semicolon, 0 to any number of characters, linefeed
Sections to ignoreany
Sections to translateany
Strings and characters to ignore
https://.*?[ \n]
/*(.?\n)*/
".*"
Links,

comments within /* and */,

comments that start with //,

strings enclosed in quotes in the value of a section

Execution

Now select the source language and one or more target languages of the text to be translated and drag the folder with the source file into the program. It is important that the name of the source file contains the code of the source language, e.g. MyAppText-EN.strings.

I downloaded the file https://github.com/coteditor/CotEditor/blob/main/CotEditor/en-GB.lproj/Localizable.strings from CotEditor for this test and renamed it to en.strings.

By clicking the buttons  Estimate usage  and then  Simulate translation  the simulation window of the program is displayed.

The left part of this window displays the original text and the right part displays the text of the simulated translation. The meaning of each color:

  • Comments
  • Key elements
  • Text to be translated
  • Translated text
  • Text excluded from translation

Since this is a simulation, the text in the right part of the window is displayed in the original language. Comments are not translated and are not inserted into the translated text because  ☑︎ Take over untranslated sections  was not selected. Otherwise, the comments would be inserted into the translated text without translation, as shown in the simulation figure below.

After closing the window, the translation can be started by deselecting  ☑︎ Do not translate online, only simulate  and then clicking  Translate and save .

Translate faster

The translation of this sample text for one language takes about 2 minutes.

Why is that?

The reason for this long translation time is that each paragraph is sent individually to the server of DeepL, translated there and then sent back. Since this is a relatively large number of paragraphs, the ratio of payload to overhead is extremely unfavorable.

How to get around this?

Since the first translation does not have to be done paragraph by paragraph, the entire file can be translated at once. To do this, the contents of the Section title and Section end input fields are deleted and entered in the Strings and characters to ignore input field. Translating the entire file now takes less than 3 seconds. This is a tremendous progress.

However, for future changes to existing translations, it makes sense to use the method described above and specify the headings of the sections to be translated in the Sections to translate input field. This way, the entire text will not be translated again and the quota at DeepL will be much less burdened.

This happens with section-by-section translation when the same section headings are used multiple times in a file. In this case, only the last section is actually translated. As a rule, however, this should not be a problem, since the section title is used as a key and multiple occurrences therefore do not make sense.

Conclusion

Translate Files with AI makes it easy and fast to translate .string files into up to 30 different languages. It is just as easy to keep the translations up to date in case of later changes.

Leave a Reply

Your email address will not be published. Required fields are marked *