Blog

Automated Localization Script

automated-localization-script-1.webp

I. Introduction

When developing applications that require support for multiple languages, it is necessary to create string files that define key-value pairs. These key-value pairs consist of unique keys that are used to retrieve corresponding values, which represent the content specific to the selected language. By using this approach, developers can easily manage language-specific data and ensure that the correct content is displayed to users based on their language preferences.

Working on multi-platform projects, it is important to be aware that each platform may have its own unique string file format. For example, iOS uses the .strings format, while Android uses .xml, and Flutter uses the .json format. These file formats have different structures and syntax, which can make it challenging to manage language-specific data across multiple platforms.

II. Problem

There are numerous issues that developers may encounter when working on multilingual projects, including:

  • Collaborators editing the same string file may lead to conflicts or duplicate keys.
  • The teams (iOS and Android) do not agree on keys. Usually, developers do not decide the values but rely on translators or clients to provide them. If the keys are not the same, it causes many difficulties.
  • For each key, the developer needs to provide temporary values for the corresponding language without knowing the meaning by using Google Translate to translate and then copy into each file.

III. Solution

Google Sheets provides an ideal platform for collaboration on a single file, as it enables multiple users to access and edit the same file simultaneously. By leveraging the power of Google Sheets, developers can work together on a single file in real-time, thereby streamlining the collaboration process and enabling more efficient workflows. This approach can be particularly helpful for multi-person projects, as it allows team members to view and modify the same data, share feedback, and make updates as needed.

Define key-value pairs for English.

In mobile development, localization strings files are typically used to provide translations for app content in different languages. Key-value pairs are a common way to define these translations in these files.

The key is a unique identifier that represents the content to be translated, while the value is the translated text for that key.

key value pairs

Write a script to automatically generate content for the corresponding string files for each language.

What is Google Apps Script?

Google Apps Script is a platform for developing applications quickly, designed to facilitate the creation of business applications that can be seamlessly integrated with Google Workspace. By using modern JavaScript, developers can easily write code that leverages the built-in libraries available for popular Google Workspace applications, including Gmail, Calendar, Drive, and many more.

Access the Apps Scripts editor

google apps script

By utilizing Google Apps Script, developers can write functions to convert key-value pairs into string files within Google Sheets, with the script being based on JavaScript

We define 3 functions for 3 platform (iOS, Android, Flutter), each function will have 3 parameters

  • Screens: all values of the screen name.
  • Keys: all values of the keys
  • Values: all values of the value

The function is designed to merge values with their corresponding keys and screens, creating a final output that accurately reflects the original data. To accomplish this, the function loops through all values and matches them with their respective keys and screens before merging them together.

For strings with internal formats, we convert them to the correct format for each platform using the replace function.

  • For example, %s will be converted to %@ on iOS.

 

Code

iOS 

code ios

Android

code android

Flutter

code flutter

Back to Google Sheet file, using the corresponding functions for iOS, Flutter, and Android.

corresponding funtions

cocalizeios

For other languages, we use Google Translate to translate and then use the localize function above to generate the corresponding string files. When using GOOGLETRANSLATE to convert text from one language to another, it is necessary to specify the two-letter language codes of both the source and target languages as the 2nd and 3rd parameters.

corresponding funtions 2

After completing the necessary steps to generate values for iOS, Android, and Flutter, the next action is to copy these values into a text editor. By doing so, developers can easily view and compare the results side-by-side, making it easier to identify any discrepancies or errors that may require further attention.

automated localization script 2

Automatically update the corresponding values for each file.

Having successfully generated the string files, the next challenge is to automate the updating process whenever the files are edited. Manually copying and pasting values every time a change is made can be a tedious and error-prone task, which is why we leverage the tool `gspread` (https://github.com/burnash/gspread) to streamline this process. By utilizing `gspread`, developers can automate the synchronization of data between the string files and the corresponding values, thereby reducing the risk of human error and saving valuable time and resources. This automation ensures that the project remains up-to-date and accurate, while also enabling a more efficient and streamlined development process.

automatically update the corresponding values

automatically update the corresponding values 2

After installing `gspread`, we create a tools folder in each corresponding platform, where we have 2 files updateLocalization.py and updateLocalization.sh

  • updateLocalization.py is used to access the Google Sheet file and copy the desired cell value. The Column value is determined by the first parameter of the argument vector, and the Row value is the same as the Row with the cell value gspreadIOS. 
  • updateLocalization.sh is used to call updateLocalization.py to get the value and copy it to the string file.

Every time we need to update the content of the string file, we only need to run a single command.

automatically update the corresponding values 3

updatelocalization

automatically update the corresponding values 4

IV. Conclusion

The use of Google Sheets and Scripts can greatly simplify the process of working with multiple languages in mobile development, as well as in front-end and back-end development. This approach offers a convenient and efficient way to manage language-specific data and automate the synchronization of data between various platforms. By leveraging the power of Google Sheets and Scripts, developers can streamline their workflows, reduce the risk of errors, and ensure consistency and accuracy across all platforms. This approach can be especially beneficial for multilingual projects or projects that involve frequent updates and changes, as it allows developers to easily manage and update language-specific data in a centralized location.

 

Resources

Content Manager

Thanh (Bruce) Pham CEO of Saigon Technology

A Member of Forbes Technology Council

Table of Contents

Want to start a project?

Our team is ready to implement your ideas. Contact us now to discuss your roadmap!

get in touch

As a Leading Vietnam Software Development Outsourcing Company, we dedicate to your success by following our philosophy:

YOUR SUCCESS IS OUR MISSION.

about-us

Contact Us