In Flutter I want to set Locale to use apart from English, also Serbian language. Problem is that for English it's easy and it goes like this:
Locale('en', 'US')
And it includes regular file in lang folder named en.json
But for Serbian locale there are Cyrillic and Latin, and I need 2 files included, and I need 2 locales loaded. From the Flutter docs for Locale I've found this and it says:
Type: variant
Subtag: ekavsk
Description: Serbian with Ekavian pronunciation
Prefix: sr
Prefix: sr-Latn
Prefix: sr-Cyrl
Added: 2013-12-02
I've tried loading it with
Locale('sr-Cyrl', 'RS')
And naming the file sr-Cyrl_rs.json but that crashes with log message Warning: This application's locale, sr-Cyrl_RS, is not supported by all of its localization delegates.
What am I missing?
For what it's worth, not sure if you managed to solve the issue, but for anyone looking for this, I managed to solve it like this.
Definitely follow the instruction here: https://flutter.dev/docs/development/accessibility-and-localization/internationalization
And for Serbian Cyrillic and Latin ones, my setup was like this:
app_en.arb
app_sr_Cyrl.arb
app_sr_Latn.arb
app_sr.arb <- fall-back language that is needed
Now when this get's compiled for the first time, it will generate a single file for the Serbian language: app_localization_sr.dart and if you look at that file in the build directory, you will see that it basically switches between different script types.
Now the tricky part was figuring out how to "load" the correct one. Since I wanted to load the Cyrillic one first, for the Material App I used this pies of code:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Title',
locale: Locale.fromSubtags(languageCode: 'sr', scriptCode: 'Cyrl'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Container(),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With