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.
Why Avoiding Hardcoded Strings Enhances App Scalability?
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.
Top Flutter Translation Tools: Paid vs Free Options
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.
Leveraging Google Sheets for Cost-Effective App Translations
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.
.arb vs .json: Best File Types for Flutter 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.
Guide to Using Google Sheets Template for Translations
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.
Automating Translations with Dart
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.
Breaking Down the Dart Script for Efficient Translations
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:
Setting Up Constants
In the beginning, two constants are defined:
- _csvUrl, which points to the Google Sheets .csv link.
- _fileName denotes the file name where the downloaded CSV will be temporarily stored.
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.
Preparing for Conversion
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 languages list will store the languages that the CSV file contains (English, Spanish, etc.).
- The translations map will hold the translations, with each language as a key, pointing to another map that contains key-value pairs of translations.
Reading and Parsing the CSV
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.
Converting to .arb Files
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.
Boosting Flutter Translations with the Intl Package
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.
Do you want to find out more about Flutter app development?
Choosing the Right Translation Strategy for Your Flutter Application
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.