Do You Speak-A My Language?!
Tags: programming, flutter
Published: 2023-09-07 22:23:51

---
title: Do You Speak-A My Language?!
published: true
date: 2019-10-25 18:45:40 UTC
tags: programming, flutter
canonical_url: https://medium.com/@greg.perry/do-you-speak-a-my-language-587854c2d0a3
---

_Supply Translations to your Flutter app._

When it comes to “Internation-­alizing” your Flutter app, there’s a few things you have to set up first before you can proceed as it’s states in the documentation, [Setting up an internationalized app](https://flutter.dev/docs/development/accessibility-and-localization/internationalization?source=post_page-----ef1e9e80ec58----------------------#setting-up-an-internationalized-app-the-flutter_localizations-package):

> “To add support for other languages, an application must…include a separate package called flutter\_localizations.… If you want your app to work smoothly on iOS then you have to add the package ‘flutter\_cupertino\_localizations’”

[![](https://cdn-images-1.medium.com/max/1024/1*Aq16IZeJO7rAgt1MBwfJgg.png)](https://flutter.dev/docs/development/accessibility-and-localization/internationalization?source=post_page-----ef1e9e80ec58----------------------#setting-up-an-internationalized-app-the-flutter_localizations-package)<figcaption><a href="https://flutter.dev/docs/development/accessibility-and-localization/internationalization?source=post_page-----ef1e9e80ec58----------------------#setting-up-an-internationalized-app-the-flutter_localizations-package">Setting up an internationalized app</a></figcaption>

With that, you can then import the flutter\_localizations library. Next, you must specify the ‘localizationsDelegates’ and ‘supportedLocales’ to the widget, _MaterialApp_. Each delegate encapsulates a collection of localized values or, with regards to this article, localized text translations.

[![](https://cdn-images-1.medium.com/max/1024/1*mMpTy8ALVvPz1E1DOlUEew.png)](https://flutter.dev/docs/development/accessibility-and-localization/internationalization?source=post_page-----ef1e9e80ec58----------------------#setting-up-an-internationalized-app-the-flutter_localizations-package)<figcaption><a href="https://flutter.dev/docs/development/accessibility-and-localization/internationalization?source=post_page-----ef1e9e80ec58----------------------#setting-up-an-internationalized-app-the-flutter_localizations-package">Setting up an internationalized app</a></figcaption>

Flutter has a process to provide such translations ([localization strings](https://github.com/flutter/flutter/blob/master/packages/flutter_localizations/lib/src/l10n/README.md)), but I preferred [Thomas Ecalle](https://blog.smartnsoft.com/@thomas_49096?source=post_page-----ef1e9e80ec58----------------------)’s own library, [FlappyTranslator](https://github.com/smartnsoft/FlappyTranslator), as it too provided a means to display native text strings and their translated text strings in a Flutter app. His work inspired me to create the library package, [I10n\_Translator](https://pub.dev/packages/i10n_translator). One particular characteristic that I liked and blatantly adapted from his library to use in my own, as he writes in his article, “our strings need to be stored in a **human-readable** and **easy-to-use-or-update** format for non-developers.”

[Flutter Internationalization : the easy way with flappy\_translator](https://blog.smartnsoft.com/flutter-internationalization-the-easy-way-with-flappy-translator-ef1e9e80ec58)

His library simply provided a text file that contains comma-separated values. This is commonly called a CSV file. It’s a text file that uses a comma to separate its text values, and is readily recognized by many editors and other popular programs. Thus allowing ‘non-developers’, for example, to easily enter the text translations for a Flutter app. And so, the library package, [I10n\_Translator](https://pub.dev/packages/i10n_translator), also uses a CSV file to store text translations.

As an example, below is a screenshot of the CSV file named, _i10n.csv_, containing some English words and their corresponding French and Spanish translations in the simple Windows text editor called, _Notepad_.

[![](https://cdn-images-1.medium.com/max/1024/1*TY5b-D-6g_CeOVyDc0SpEw.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv">i10n.csv</a></figcaption>

As you see below, this text file is easily read in by Microsoft Excel presenting its contents in a nice easy-to-read tabular format. You can see the English column, the French column and the Spanish column. Further, you can recognize it’s the first line of text displayed is the line that contains the ‘language codes’ ([ISO 639–1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)) represented in this CSV file. Lastly, it also has blank lines here and there, again, for readability.

[![](https://cdn-images-1.medium.com/max/1024/1*hR36GlIjEUCUMmRwqLwk-g.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv">i10n.csv</a></figcaption>

### Screenshots. Not Gists.

In this article, I’m going to quickly introduce this library package and how to use it. As always, I prefer using screenshots over gists to show code in my articles. I find them easier to work with, and easier to read. However, you can click/tap on these screenshots to see the code as a gist or in Github if you like. Ironically, it’s better to read this article about mobile development on your computer than on your phone. Besides, we program mostly on our computers; not on our phones. For now.

### No Moving Pictures On Social Media

Further, there‘s a few _gif_ files in this article demonstrating functions and features in ‘moving pictures.’ However, I’m told viewing such files is not possible when reading this article on platforms like Instagram, Facebook, etc. You’ll see empty square spaces where the _gif_ files should be. Please, be aware of this. I would recommend reading this in a browser on medium.com.

Let’s begin.

[![](https://cdn-images-1.medium.com/max/1024/1*uUQIQZId3hfjkaaS4wMzoQ.png)](https://medium.com/@greg.perry)<figcaption><a href="https://medium.com/@greg.perry">Other Stories by Greg Perry</a></figcaption>

### Learn by Example and Sample App

The CSV file named, _i10n.csv_, you’ve see above is actually used by the sample app, [Shrine MVC](https://github.com/Andrious/shrine_mvc). This sample app, of course, is available to you on Github which you can download so to understand how this library package can be used in your own Flutter app. Note, the library package itself has its own [example](https://pub.dev/packages/i10n_translator#-example-tab-) app as well to further demonstrate its implementation. The source code of the example app is also found on [Github](https://github.com/AndriousSolutions/i10n_translator/tree/master/example).

### Generate Your Translations

Below is a screenshot of the resulting code generated from the CSV file. Of course, if you’re familiar with [my past library packages](https://pub.dev/packages?q=email%3Asupport%40andrioussolutions.com), you know I like options. In this case, you’ve the option to instead ‘read in’ the CSV file directly to provide the translations! You don’t have to generate this code from the CSV file if you don’t want to. I’ll explain that later on. For now, let’s see how you’d use this code anyway as there may be times when you’ll want to generate it.

[![](https://cdn-images-1.medium.com/max/1024/1*EKX0-GaPYxIY7uZpvLBjkg.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/home/model/i10n_words.dart)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/home/model/i10n_words.dart">i10n_words.dart</a></figcaption>

In the screenshot above, there’s now a Map object called, _i10nWords_, that contains all the translations currently available to the Flutter app. Below, is a screenshot of the sample app, [Shrine MVC](https://github.com/Andrious/shrine_mvc), taking in that object with an _import_ statement and then supplying that object to the **init** () function in the i10n\_translator library package. Easy peasy.

[![](https://cdn-images-1.medium.com/max/1024/1*i83i8MuosZa2_5yjzfnYFQ.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/app/controller/app.dart#L24)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/app/controller/app.dart#L24">app.dart</a></figcaption>

### Change Your Language

As it happens, when you run the sample app, [Shrine MVC](https://github.com/Andrious/shrine_mvc), you’ve the means to manually ‘switch’ between the default text and its two translations. There’s a menu option allowing you to do just that. The screenshots below depict the change, for example, from English to French. Notice the labels of the displayed articles then change accordingly.

![](https://cdn-images-1.medium.com/max/1024/1*-9e-V17lEU5LBgw5aa86uw.png)

Below is the screenshot of the code that displays those items and subsequently they’re corresponding labels. You can see the default strings, in this case, are in English. However, note when looking at the list of _Product_ objects below, the named parameters called, _name_, are not being supplied the strings directly but are instead being supplied the static function, **s** (), from the library package, _i10n\_translator_. It’s in this static function where the translation is performed and returned to the named parameter, _name_.

[![](https://cdn-images-1.medium.com/max/1024/1*NRxY87t6ikQL5iCpSu_qGw.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/home/model/products_repository.dart#L17)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/home/model/products_repository.dart#L17">ProductsRepository class</a></figcaption>

### Shown In Pictures

Below is a _gif_ file demonstrating how the app’s text dynamically changes when you select a ‘language code’ in the menu popup. Note, instead of ‘language code’, I could have just as easily used flags or what have you. It didn’t have to be a list of language codes. Regardless, it’s the library package that takes in the language code and change the Locale of the app accordingly.

![](https://cdn-images-1.medium.com/max/612/1*1dwUf6jPwlWvRCbV-Ak8GA.png)

![](https://cdn-images-1.medium.com/max/960/1*ZBJGs93q1bI9_LyHn_nGLw.gif)

![](https://cdn-images-1.medium.com/max/612/1*1dwUf6jPwlWvRCbV-Ak8GA.png)

### Load Your Locale

The screenshot below conveys the PopupMenuButton widget with its three vertical dots displayed in the _gif_ file above. Notice how the I10n\_translator library package is called upon to load the selected Locale. Consequently, in the code, that value is then saved in the app’s system preferences so to load that selected locale if and when the app starts up again. Finally, the app’s interface is refreshed to present the new text translations.

Note, further along in the code, I could have read the generated object, _i10nWords_, and listed the ‘language codes’ it contained instead of just explicitly listing PopupMenuItems in the code, but I was lazy.

[![](https://cdn-images-1.medium.com/max/1024/1*bNTMmIFOoiKj3SMHG2uakQ.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/home/view/backdrop.dart#L333)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/home/view/backdrop.dart#L333">backdrop.dart</a></figcaption>

Again, the idea behind all this is to provide the app’s translations in a simple text file delimited by commas. It’s assumes the ‘default’ language is found in the first column. You would then insert those text strings throughout the app itself as you normally would. However, instead of using the widget, _Text_, you would insert those text strings into the static functions, I10n. **s** () or I10n. **t** (). The first column then serves as the ‘key field’ when reading in this CSV file. Note, as a key field, the first column of text is therefore case-sensitive.

[![](https://cdn-images-1.medium.com/proxy/1*hR36GlIjEUCUMmRwqLwk-g.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv">i10n.csv</a></figcaption>

### Set Up To Generate

Once the translations are ready, you’ve the option now to generate the code and supply those translations to your Flutter app as a parameter. Note, to use this option, you have to set up one thing in your app’s pubspec.yaml file. Under the header, _dev\_dependencies_, you have to reference the library package so you can call it within your chosen IDE.

[![](https://cdn-images-1.medium.com/max/1024/1*3WIJHjcxvyl9PHB0Bxl6NQ.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/pubspec.yaml#L36)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/pubspec.yaml#L36">pubspec.yaml</a></figcaption>

And so now using your IDE’s terminal window for example, you’re free to type in the following command line to generate the code in a Dart file:

**flutter pub run i10n\_translator test.csv path/destination/results.dart**

The command line above, as an example, would read in a CSV file called, _test.csv_, and produce a Dart file called, _results.dart_, in the specified location. It will contain the Map object, _i10nWords_. Note, however, if you simply type out the command line without the two arguments, it assumes the following CSV file and resulting Dart file:

**flutter pub run i10n\_translator [i10n.csv] [i10nwords.dart]**

Further still, however, if you’re innately lazy and don’t like typing like me, you could use Android Studio and create a ‘Dart command line app’ that instead points directly to the library package file, _i10n\_translator.dart_. You then just ‘run’ that configuration to produce the resulting Dart file. See below.

![](https://cdn-images-1.medium.com/max/1024/1*UY-pMbF3rowgUyccMUAMEg.png)

![](https://cdn-images-1.medium.com/max/1024/1*iwUPdT7q62R1efWV153f9g.png)

### How Does It Generate?

So, how does that command line work? Looking at a screenshot of that i10n\_translator.dart file, you can see there’s a **main** () function in there that calls the library package class, _I10nTranslator_. Further, it supplies any arguments to the function, **generate** () that creates the code.

[![](https://cdn-images-1.medium.com/max/1024/1*l1FSDYiWdWLz7zV6wL5xjQ.png)](https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/lib/src/i10n_translator.dart#L215)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/lib/src/i10n_translator.dart#L215">i10n_translator.dart</a></figcaption>

### Just The CSV!

Again, I’m lazy. I don’t want to do that ‘extra step’ of generating the code to display the translations. I’ve got this CSV file now, why don’t I just use that file?? To do that, I’ll just call the **init** () function with no parameters.

[![](https://cdn-images-1.medium.com/max/1024/1*3tUKFnx_iSmO1MJeHWeMkw.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/app/controller/app.dart#L24)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/lib/src/app/controller/app.dart#L24">app.dart</a></figcaption>

You see, I’ve simply included the text file as an asset in the Flutter app and have the library package read it in at start up to supply the translations. By default, this library package will look for the CSV file in the assets directory under the location, **assets/i10n/i10n.csv**. And so, instead of adding the library package under the header, _dev\_dependences_, I’ve specified that location in the pubspec.yaml file. When the app starts up, that CSV file will be read in and the translations made available to the Flutter app. Easy peasy.

[![](https://cdn-images-1.medium.com/max/1024/1*6nzfvpP7D7mpHhrVWfhWbQ.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/pubspec.yaml#L46)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/pubspec.yaml#L46">pubspec.yaml</a></figcaption>

And so, over time for example, while your developing your app, you may have more translations entered into that CSV file. There’s no need to generate any code from time to time. Merely place that file in the ‘assets’ location and you’re good to go!

[![](https://cdn-images-1.medium.com/max/1024/1*cryMdbjrNV8Wt217yJfyPg.png)](https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv)<figcaption><a href="https://github.com/Andrious/shrine_mvc/blob/92e389920b3133025a95d575131dae6661dd1552/assets/i10n/i10n.csv">i10n.csv</a></figcaption>

### No Words

Now what happens if there’s no translation available for a particular word in your Flutter app? Nothing happens. It simply displays the original key field — which happens to be the original text. See what I mean?

This is a utility class, it’s not to hinder your own Flutter app. After all, it’s your Flutter app! An app, no doubt, designed for a very important task! It’s that task that is of primary concern. If there happens to be a ‘missing translation’, you certainly don’t want your app to blow up because of it! So it won’t. It’ll just display the string supplied to the static function I10n. **s** () or I10n. **t** ().

If you’ve implemented those static functions throughout your app and yet there’s no CSV file for example, there’s no problem. Again, they’ll simply display the passed text string. Further, even if you mistakenly pass nothing to those static functions, an empty string is then displayed…unseen of course.

[![](https://cdn-images-1.medium.com/max/1024/1*5iHFV7UuvhBa9MwfEAHBZg.png)](https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/lib/src/i10n.dart#L164)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/lib/src/i10n.dart#L164"><strong>s</strong>() function</a></figcaption>

### Better Late Than Never

Like documentation, translations tend not to get done as the development progresses in projects. Frankly, like documentation, it tends only to get done at the last minute! Again, the priority is getting the app working; not its translations. This library package allows for that. With their unique signature, you search your code and quickly find all the static functions and collect all the text that needs translating. Better yet, have the library package do that for you — but, I’ll explain that a litter later.

![](https://cdn-images-1.medium.com/max/1024/1*kyIAJiwCDfv5kA5b-sJ9Tg.png)

### Add Your Delegates

Like the sample app, [Shrine MVC](https://github.com/Andrious/shrine_mvc), the example included with the library package has to first pass the ‘supported locales’ and ‘localization delegates’ to the MaterialApp widget whenever a Flutter app requires translations. Again, this has to be done if you want to introduce Internationalization into your Flutter app — even without using the library package, _i10n\_translator_. Both the entries, **I10nDelegate()**and **I10n.supportedLocales** are supplied by the library package, _i10n\_translator.dart_, and so it’ll take care of that part for you.

[![](https://cdn-images-1.medium.com/max/1024/1*mFxI0CMJLVP0IxUXeUbNZg.png)](https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/example/lib/main.dart#L15)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/example/lib/main.dart#L15">main.dart</a></figcaption>

### Make The Switch!

As it happens, its example code also has a menu option that allows you to switch between languages. In this case, between eight different locales. Once selected, like the first sample app, [Shrine MVC](https://github.com/Andrious/shrine_mvc), the code calls the library package to load the selected locale, it then saves that locale into system preferences, and then refreshes the UI. Note, the saving of the selected locale uses another library package of mine called, [_Prefs_](https://pub.dev/packages/prefs)_._

[![](https://cdn-images-1.medium.com/max/1024/1*-E7lYddbx2ICyIBczsoiQg.png)](https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/example/lib/main.dart#L110)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/example/lib/main.dart#L110">main.dart</a></figcaption>

### Await The CSV

The example also looks up the file, **assets/i10n/i10n.csv** , for its translations. Since reading a file in Flutter involves an asynchronous operation, that means it involves Future objects. Hence, the example, in this case, needs time to ‘read in’ the CSV file before it can proceed. As you see below, that’s why a FutureBuilder widget is used right away in the example.

[![](https://cdn-images-1.medium.com/max/1024/1*p5qDx3TuGKqBEqBDQA6PvQ.png)](https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/example/lib/main.dart#L1)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/example/lib/main.dart#L1">main.dart</a></figcaption>

### It Can’t Wait

Now, as I suggested earlier, there will be instances where you don’t want to wait for a file to be read in. If you recall, it’s in those instances where you’ll want to generate that Map object called, _i10nWords_. For example, if a FutureBuilder widget is not to be used, the example code could look like this:

[![](https://cdn-images-1.medium.com/max/1024/1*_kbfvbe1eqEZc149fo2wpA.png)](https://github.com/AndriousSolutions/i10n_translator/blob/c15d2d5047ec79d31a4ab0d795dfefc014706c03/example/lib/main02.dart#L1)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/c15d2d5047ec79d31a4ab0d795dfefc014706c03/example/lib/main02.dart#L1">main02.dart</a></figcaption>

After using the i10n\_translator file to generate the code, the library package’s **init** () function is called like before, but now the generated code in the file, _i10n\_words.dart_, is instead read in as a parameter. This, of course, is a much faster operation and yet produces the very same results at the first version. Click on the example code’s menu option, and you can change the text to eight different languages in the good ol’ counter demo app. Easy peasy.

![](https://cdn-images-1.medium.com/max/1024/1*vOf7Iso1xENLd74clW3MZg.png)<figcaption>eight locales for the counter demo</figcaption>

### Make A List

I had hinted earlier that, ideally, while you’re developing your app, this library package should just collect all the text that needs translating for you and put it in that CSV file for your translators to work on later. Well, the library package does just that — almost. As you see in the screenshot below, the **s** () function does indeed ‘add’ the text string (the key) passed to it into a file when the app is running, but there’s a problem.

[![](https://cdn-images-1.medium.com/max/1024/1*6rxbxNE29-svcqY5nsYtrA.png)](https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/lib/src/i10n.dart#L180)<figcaption><a href="https://github.com/AndriousSolutions/i10n_translator/blob/dd243bc5f6a959aa20d3e787847307939920f7b1/lib/src/i10n.dart#L180">i10n.dart</a></figcaption>

Like you, I still have a lot to learn from this wonderful platform that is Flutter. And so, as of writing, there’s an CSV file filling up with all this text, but I can’t figure out how to then readily gain access to that file. I would have liked to have resolved this before publishing this article, but...you know…I’m lazy.

Please, if you’re up for it, let me know what would be the best approach to provide that file to the developer? Better yet, to a non-developer making the process…you know…Easy peasy.

Cheers.

[→ Other Stories by Greg Perry](https://medium.com/@greg.perry)