Skip to content

flutter i18n #12

Open
Open
@jindada

Description

@jindada

中文

step 1

install Android Studio plugin Flutter_i18n(current version is 0.2)

step 2

installation is complete, restart Android Studio, The res/values folder appears below the flutter project

step 3

Clicking the Synchronization button automatically generates lib/generated/i18n.dart

step 4

edit main.dart, Because thelib/generated/i18n.dart file has bug, so localizations Delegates and localeResolutionCallback part of the code needs to be rewritten

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: <Locale>[
        Locale("en", ""),
        Locale("zh", ""),
        Locale("fr", ""),
        Locale("de", ""),
      ],
      localeResolutionCallback: (Locale locale, Iterable<Locale> supported) {

        bool isSupported(Locale locale) {
          if (locale == null) return false;
          if (supported.contains(locale)) {
            return true;
          } else {
            for (Locale supportedLocale in supported) {
              if (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)
                if (supportedLocale.languageCode == locale.languageCode) return true;
            }
            return false;
          }
        }

        if (!isSupported(locale))
          return supported.first;

        final Locale languageLocale = Locale(locale.languageCode, "");
        if (supported.contains(locale)) return locale;
        else if (supported.contains(languageLocale)) return languageLocale;
        else return supported.first;
      },
      home: MyHomePage(),
    );
  }
}

IOS requires additional steps
https://flutter.dev/docs/development/accessibility-and-localization/internationalization#appendix-updating-the-ios-app-bundle

step 5

restart app

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions