I have set the flutter_localizations package in 0.2.0 version. My main.dart is set with the locale parameters for this way.
class MainAppState extends ConsumerState<MainApp> {
//Variable del localization del dispositivo
final FlutterLocalization localization=FlutterLocalization.instance;
//Variable del sharedprefereces
final keyValueStorageService=KeyValueStorageServiceImpl();
@override
void initState() {
super.initState();
//Cargamos los lenguajes soportados para la app
configureLocalizaton();
}
@override
Widget build(BuildContext context) {
//Variables de diseño
final size=MediaQuery.of(context).size;
//Variable del router
final appRouter=ref.watch(routerProvider);
return MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: localization.localizationsDelegates,
supportedLocales: localization.supportedLocales,
locale: localization.currentLocale,
localeResolutionCallback: (locale, supportedLocales) {
for (var supportedLocale in supportedLocales) {
if(supportedLocale.languageCode==locale?.languageCode)return supportedLocale;
}
return supportedLocales.first;
},
home: AnimatedSplashScreen(
splash: Center(
child: Lottie.asset( 'assets/animations/splashscreen_animation.json',
width: size.width,
height: size.height,
fit: BoxFit.cover
),
),
splashIconSize: 4500,
centered: true,
nextScreen: MaterialApp.router(
debugShowCheckedModeBanner: false,
theme: AppTheme(selectedColor: 1,isDarkMode: true).getTheme(),
routerConfig: appRouter,
),
backgroundColor: colorList[0],
)
);
}
//*Metodo inicial para configurar los idiomas
void configureLocalizaton(){
localization.init(
mapLocales: locales,
initLanguageCode: 'es');
localization.onTranslatedLanguage=onTranslatedLanguage;
}
void onTranslatedLanguage(Locale? locale) async{
//Leemos el provider con el locale capturado
ref.read(localeProvider(locale!.languageCode).notifier).checkLanguage;
setState(() { });
}
}
The languagecode showed ‘es’ and it’s right, but when the welcome screen is showed and the getText method is called with the context, the languagecode is ‘en’. Seems like the default value ‘en’ with countrycode ‘US’.
The LocaleData class with all texts is the next.
// ignore_for_file: constant_identifier_names
import 'package:flutter/material.dart';
import 'package:flutter_localization/flutter_localization.dart';
const List<MapLocale> locales=[
MapLocale('es', LocaleData.ES),
MapLocale('en', LocaleData.EN),
];
mixin LocaleData{
static const String welcomeScreen_welcome='welcomeScreen_welcome';
static const String welcomeScreen_begin="welcomeScreen_begin";
static const String welcomeScreen_question="welcomeScreen_question";
static const String welcomeScreen_signinB="welcomeScreen_signinB";
static const String welcomeScreen_signupB="welcomeScreen_signupB";
static const String siginScreen_title="siginScreen_title";
static const String siginScreen_welcome="siginScreen_welcome";
static const String siginScreen_rememberP="siginScreen_rememberP";
static const String siginScreen_forgottenP="siginScreen_forgottenP";
static const String siginScreen_signinB="siginScreen_signinB";
static const String siginScreen_signupRRSST="siginScreen_signupRRSST";
static const String siginScreen_withoutAccountT="siginScreen_withoutAccountT";
static const String siginScreen_signupT="siginScreen_signupT";
static const String signUpScreen_title="signUpScreen_title";
static const String signUpScreen_fullNameT="signUpScreen_fullNameT";
static const String signUpScreen_mailT="signUpScreen_mailT";
static const String signUpScreen_passwordT="signUpScreen_passwordT";
static const String signUpScreen_rpasswordT="signUpScreen_rpasswordT";
static const String signUpScreen_agreeT="signUpScreen_agreeT";
static const String signUpScreen_conditionsT="signUpScreen_conditionsT";
static const String signUpScreen_signUpB="signUpScreen_signUpB";
static const String signUpScreen_alreadySignupT="signUpScreen_alreadySignupT";
static const String signUpScreen_signInT="signUpScreen_signInT";
//Mappers
static const Map<String,dynamic> ES={
welcomeScreen_welcome: 'Bienvenido a Roll&Play',
welcomeScreen_begin: "Comencemos con tu cuenta",
welcomeScreen_question: "¿Qué quieres hacer?",
welcomeScreen_signinB: "Login",
welcomeScreen_signupB: "Registrar",
siginScreen_title: "Login",
siginScreen_welcome: "Bienvenido de nuevo",
siginScreen_rememberP: "Recordarme",
siginScreen_forgottenP: "¿Olvidaste la password?",
siginScreen_signinB: "Entrar",
siginScreen_signupRRSST: "Loguea con",
siginScreen_withoutAccountT: "¿No tienes cuenta?",
siginScreen_signupT: "Registrate aquí",
signUpScreen_title: "Comencemos",
signUpScreen_fullNameT: "Nombre completo",
signUpScreen_mailT: "Email",
signUpScreen_passwordT: "Password",
signUpScreen_rpasswordT: "Repita la password",
signUpScreen_agreeT: "Acepto los términos y condiciones de la",
signUpScreen_conditionsT: "Política de la aplicación",
signUpScreen_signUpB: "Registrar",
signUpScreen_alreadySignupT: "¿Ya tienes cuenta?",
signUpScreen_signInT: "Ingresa aquí?"
};
static const Map<String,dynamic> EN={
welcomeScreen_welcome: "Welcome to Roll&Play",
welcomeScreen_begin: "Let's begin with you account",
welcomeScreen_question: "What do you want?",
welcomeScreen_signinB: "SignIn",
welcomeScreen_signupB: "SignUp",
siginScreen_title: "SignUp",
siginScreen_welcome: "Welcome back",
siginScreen_rememberP: "Remember me",
siginScreen_forgottenP: "Forget password?",
siginScreen_signinB: "SignIn",
siginScreen_signupRRSST: "Sign up with",
siginScreen_withoutAccountT: "You don't have an account?",
siginScreen_signupT: "Sign up here",
signUpScreen_title: "Let's begin!",
signUpScreen_fullNameT: "Full name",
signUpScreen_mailT: "Email",
signUpScreen_passwordT: "Password",
signUpScreen_rpasswordT: "Repeat the password",
signUpScreen_agreeT: "I agree to the processing of",
signUpScreen_conditionsT: "Personal data",
signUpScreen_signUpB: "SignUp",
signUpScreen_alreadySignupT: "You have already an account?",
signUpScreen_signInT: "Ingresa aquí?"
};
static String getText(BuildContext context, String key) {
String locale = Localizations.localeOf(context).languageCode;
if (locale == 'es') {
return ES[key] ?? key;
} else if (locale == 'en') {
return EN[key] ?? key;
} else {
return key;
}
}
}
In the welcome_screen
Center(
child: RichText(
textAlign: TextAlign.center,
text: TextSpan(
children: [
TextSpan(
text: LocaleData.getText(context, LocaleData.welcomeScreen_welcome),
style: textstyle.titleLarge!.copyWith(fontSize: 45.0,fontWeight: FontWeight.w600) ),
TextSpan(
text:
LocaleData.welcomeScreen_begin,
style: textstyle.titleMedium!.copyWith(fontSize: 25,color: Colors.white)),
TextSpan(
text:
LocaleData.welcomeScreen_question,
style: textstyle.titleMedium!.copyWith(fontSize: 20,color: Colors.white54)),
],
),
),
),
Someone can help me please?