Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I would like to retrieve from my flutter/dart project, the current system lqnguage code, like. fr, en, ...

What I have tried:

I have tried the following:


Locale myLocale = Localizations.localeOf(context);
String lc = currentLanguageCode(myLocale);

print ("Language code: ${lc}");


The problen is from the void initState() statement, the context is null and will then return an empty string.

I was hoping to have a method like:
Platform.isIOS


Does anyonce have an idea how to grab this language code from everywhere in the project.

Thank you very much in advance.
Best regards.
MiQi
Posted
Updated 3-Feb-21 0:36am
Comments
Kornfeld Eliyahu Peter 5-May-19 5:39am    
Maybe this one: https://proandroiddev.com/flutter-localization-step-by-step-30f95d06018d

1 solution

use didChangeDependencies method,the method is called after the initState is called and it gives you access to the context.

Your code will look like this
@override
  void didChangeDependencies() {
    Locale myLocale = Localizations.localeOf(context);
    print('my locale ${myLocale}');
    super.didChangeDependencies();
  }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900