Alternatives for Your Easter Meal

The tradition of all the family gathering around a huge dining table just isn’t realistic anymore. You may be entertaining a small group of friends or family with paper plates rather than Grandma’s…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Flutter Localization on the Fly

flutter logo taken from flutter.dev

Lots of apps require support for multiple languages inorder to have engagements with different kinds of users and we don’t want to miss our customers just because of a language barrier.

I just googled and found that (As per 2017 report) out of 7.8 billion people only 1.35 billion people speak English so that’s approx 1/7 th of the population that means if you developed a software that targets humans only 1 out of 8 people will be able to understand it. Whether they would like it or would be interested in your app thats a different question 😂?
If you develop something with all the efforts and if people don’t understand it, its of no use. So you have to make sure your app supports the language of your targeted regions, so for this we have to make use of L10N (aka LOCALIZATION).

- But what if you have to make some changes to those Strings after you have deployed the app to stores? Or

The approach I will show you is less of a solution but more of “naive” workaround,If you know how to work with Http Requests and maintain the state of the app with provider, This would be a piece of cake for you.
And this will get the work done without making a single line of change in code once it is implemented(unless you want to add new localized Strings to the app). Unfortunately, I cannot show you that mobile app, but don’t worry I have a sample application built for this blog post which uses this approach.To Give you a test of the sample application this is how it looks.

The things that we localize in a app are basically strings,we Identify each string uniquely with a key,So basically the data which has to be dynamically changed is a JSON object. This JSON data would lie somewhere in your backend which could fetch using network request. You could also store it locally on your phone but remember we want a dynamic solution to update the languages during runtime.
if we were to localize the app in Spanish the json object would look something like this.

if we had multiple languages we could just change the values and keep the keys the same. And our consolidated JSON object would look like this.

Alternatively, we could just have one JSON object in a single response, and to get a correct translation we could just pass a query param in a request to let the backend know that we are requesting a JSON object of a particular language and it would return you the correct translation object.

so once we have this JSON object ready we could just make an HTTP request decode the response and display it on the Ui, problem solved right?
No
Also, don’t forget the variables, Sometimes we also have variables within our static strings which could be numbers, dates and other types.

for instance, consider this flutter code

so for such strings, we need to make sure these variables go within those translations strings coming from the JSON object in the right place. so for this, I wrote a simple function that does this given an input it produces the respective output.

Here's the function which does this for us.

so for the above input, we would invoke this method like this

Then it would return the output

Now that you understood the approach (I assume) let's build the sample app.
Since you are looking for a solution for localization I assume you know how to build layouts in flutter so I won't be covering that part.

so let's create the flutter project and run this command in your terminal

Now in the main.dart we need to import provider and wrap the MaterialApp in a consumer so as to rebuild the whole app when we change the language.

The LocaleModel class contains everything about localization which we are interested in and the Settings Class contains code related to changing the theme of the app, So let's focus on the LocaleModel class.

the constructor at the top invokes this function as soon as the app starts. Remember we have a multiprovider wrapped around in main.dart that's where this call comes from.

Now let's see how we are making use of these translations

Lets take a look at description for the User which has three parameters in it city,state,country

red underlined variables used in the description of the user.

The description that we receive in the API response looks like this

And this is how the description widget looks.

And thats how we can localize the entire app,The syntax might look bit ugly but we can always simplfy it.

And we can change the language of the app like this

And you can see few more additional things done in the source code like storing and fetching the current locale for the next launch using sharedPreference.

And thats it we are at the end.

This approach allows you to
- Dynamically edit the translation files.
- Add/ Remove support for a new language.
both without making a single line of change in your flutter app.
- No hassle with generating arb files and maintaining static translations

Thank you for taking your time to read this long post and keep fluttering 💙.

Add a comment

Related posts:

Bibit Rambutan Binjai Super Kota Bogor Terlengkap

bibit rambutan binjai super Menyediakan bibit buah tabulampot,tanaman bunga hidup untuk mempercantik ruangan, cocok untuk hiasan rumah maupun ngasilin buah super unggul ya. Hasil okulasi, sambung…

What makes a good VC

I get to read a lot about what VCs look for in companies/founders/teams. I like reading about it, and it is valuable. But I read very little about what founders should look for in investors. So I…

Using Salesforce Flow With Quip

Using Salesforce Flow With Quip. The new Quip Flow Actions for Salesforce Flow open up new ways to combine the shared, universal accessibility of Quip with the power of….