Table of Contents
Key Takeaways
At the heart of every user-centric application lies the user's experience. A significant aspect of this experience is language – how users understand and interact with your app. Translations, in the software domain, refer to the practice of converting the application’s textual content into multiple languages, allowing it to cater to a global audience.
When building applications, it’s a common misstep to directly hardcode strings into the code. This makes the application rigid and complicates the process of introducing new languages or changing existing content. Utilizing translations streamlines this process and fosters a scalable and maintainable codebase.
Flutter offers many translation tools, many of which come with a price tag. Some popular tools like Localizely and Crowdin provide cloud-based solutions that allow developers to manage and sync translations effortlessly. They generally function by integrating with your Flutter project, detecting new strings, and assisting in the translation process, either manually or through automated suggestions.
Paid tools, although efficient, might not be the go-to for everyone, especially those handling more minor projects. A resourceful alternative can be Google Sheets. It’s free, easily shareable among teams, and can host translations in a structured format. This way, everyone on the team can access and possibly edit the translations.
Flutter projects often utilize either .arb or .json files for translations. While both serve the purpose of storing key-value pairs for translations, they have distinct use cases. .arb files, which stand for Application Resource Bundle, are specific to the Flutter internationalization package, making it a default choice for many. On the other hand, .json files, due to their widespread acceptance in many software realms, can sometimes be more versatile and work well with various tools and platforms.
For those keen to explore, the template of the Google Sheet for translations can be found here.
It's a simple structure with columns for Keys, English translations, and Spanish translations. However, when it comes to retrieving this data, the link used is slightly different, as seen here.
This link facilitates the download of the .csv file, which becomes crucial for the next step – the Dart script.
A Dart script capable of downloading and processing these translations can be found here on GitHub.
When executed, this script initiates by downloading the translations. It then converts this data, primarily designed to be ingested in the .csv format, and saves it into .arb files. This streamlined automation makes the translation process efficient and error-free.
The provided Dart script serves as a bridge between your Google Sheets translations and your Flutter project. Here's a step-by-step explanation of how the script operates:
In the beginning, two constants are defined:
The main function begins by initiating the download of the translations. The HttpClient class is employed to make an HTTP request to the provided _csvUrl, which in response fetches the csv file. This file is then stored locally with the name specified by _fileName.
After downloading, the next phase is to convert the .csv data into a more usable format. The script sets up a couple of lists and maps to organize the translations. Here's the logic:
The script reads the CSV file line-by-line. The first line (or row) typically contains the language names, which are added to the languages list. Subsequent lines contain the actual translations. For each line, the script extracts the key (which is the first element) and then pairs it with its respective translations for each language. This data is then structured and stored in the translations map.
Now, with all translations neatly organized in the translations map, the script creates .arb files for each language. It loops through each language, sets up a file with a naming pattern like "language.arb" (e.g., "en.arb" or "es.arb"), and then writes the translations in JSON format into these files.
With all the necessary .arb files created, the temporary CSV file downloaded at the start is deleted to maintain a clean workspace.
The elegance of this script lies in its ability to automate what would otherwise be a tedious manual process. By linking your Google Sheets to this script, any time translations are updated or added to the Sheet, running the script will fetch, process, and update the corresponding .arb files in your Flutter project seamlessly.
While the aforementioned Dart script provides .arb files, a translation tool or package becomes necessary to integrate translations into your Flutter app fully. A prominent example is the intl package. It allows you to not only use translations from .arb files but also provides tools for internationalization.
Embarking on the journey of translations for any application is a venture of balancing resources and needs. For expansive applications with vast user bases, investing in paid translation tools often becomes essential to maintain quality and efficiency. However, hosting translations in Google Sheets offers a cost-effective yet efficient approach for modest to medium-sized projects. Remember, the choice lies in aligning the tool’s capabilities with the project's demands.
Looking for a partner who not only understands your challenges but anticipates your future needs? Get in touch, and let’s build something extraordinary in the world of digital health.