If you use Getx to do localization, you need to use Translations class.
Actually before we use the Translation class, we need to return loaded json in
Map<String, Map<String, string>>
format. Loaded json has to return in the above format. Then we may pass it to Message(languages:languages)
Let’s take a look at the Message class.
class Messages extends Translations {
final Map<String, Map<String, String>> languages;
Messages({required this.languages});
@override
Map<String, Map<String, String>> get keys {
return languages;
}
}
The Messages class just simply return languages as keys. It’s very important that you override the getter keys.
So before you run GetMaterialApp do this
Map<String, Map<String, String>> _languages = Map();
for(LanguageModel languageModel in AppConstants.languages) {
String jsonStringValues = await rootBundle.loadString('assets/language/${languageModel.languageCode}.json');
Map<String, dynamic> _mappedJson = json.decode(jsonStringValues);
Map<String, String> _json = Map();
_mappedJson.forEach((key, value) {
_json[key] = value.toString();
// print("${_json[key]}");
});
//en_US bn_BD
_languages['${languageModel.languageCode}_${languageModel.countryCode}'] = _json;
}
Map<String, Map<String, String>> languages = _languages;
GetMaterialApp
GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
............................................
translations: Messages(languages: languages),
.............................................
)
Here you see that, we have translations property. It actually takes the languages String format.
In short You must use Translations class if you Getx to do localization in Flutter. And it must return
Map<String, Map<String, string>> format.