journalleft.blogg.se

Flutter language localization
Flutter language localization













flutter language localization

Step 11: When the user selects a new language from the LanguageSelectorPage, it invokes the LocaleChangedCallback method of the Application class. Step 10: Replace the hardcoded supported locales in the AppTranslationsDelegate class with the ones from the Application class:Īlso replace the hardcoded supportedLocales in your MaterialApp widget under localizationDelegates:

flutter language localization

#Flutter language localization code#

We also declare the supportedLocales in the Application class so that we do not have to hardcode them at multiple places in our code Step 9: To allow our app to reflect localization changes everywhere in the app, we create an Application class that declares a LocaleChangeCallback. Step 8: Next, we need to declare our delegates and supportedLocales in the MaterialApp widget as shown below: Reloading the AppTranslations class whenever the locale changes.Providing the AppTranslations class with the newLocale selected.Checking if a locale is supported or not.Step 7: Create the AppTranslationsDelegate class which is responsible for: Step 6: Create the AppTranslations class for fetching your JSON files from the assets > locale directory.Īpart from being responsible for fetching the JSON file data, this class decodes the JSON map and returns the value from the map for the corresponding key from the String text(String key) method. Step 5: Declare the language specific JSON files in pubspec.yaml. Step 4: Add the language specific JSON files in the assets directory: Step 3: Add the localizations dependency in pubspec.yaml flutter_localizations: This would list all the available languages for your app. Step 1: Add a settings icon to the AppBar for navigating to the LanguageSelectorPage changing the phone’s language does not have an effect on your app’s language. This blog handles the use-case where you want to implement localization that is specific to your app i.e.















Flutter language localization