Flutter how to detect device language?

感情迁移 提交于 2021-02-11 15:14:15

问题


I have a question how can i detect device language and according to that when app starts initially give a proper language ? Please if possible provide an example with code.


回答1:


Calling Localizations.localeOf(context).languageCode should return you the languageCode. MaterialApp creates and uses a default Localization if not provided and then you can call it after the MaterialApp to know the language it's currently using the device and update your app with that information with your State Managament.

My advice for a simple project would be to use this plugin for the VS IDE (or this for Android Studio, it's from the same author). After installing it just add the dependencies to your pubspec.yaml

dependencies:
  // Other dependencies...
  flutter_localizations:
    sdk: flutter

  ...

  flutter_intl:
    enabled: true
    class_name: S # Optional. Sets the name for the generated localization class. Default: S
    main_locale: en # Optional. Sets the main locale used for generating localization files. Provided value should comply with ISO-639-1 and ISO-3166-1 (e.g. "en", "en_GB"). Default: en

In Android Studio (I don't really know where in VS sorry) check Tools > Flutter intl (it should be at the end) and there you can initialize your project and add locales. There should be now a folder in your project lib/l10n with arb files, they look like JSON files, just add a a key with string. Create one for each locale you want and give them the string aprameters for each language

intl_en.arb { "name": "I have a Name" }

intl_es.arb { "name": "Yo tengo un Nombre" }

It should run the build automatically. After that create your Material App delegating S (the name of the code that will be generated, you can change the name later if you want).

import 'package:flutter_localizations/flutter_localizations.dart';
import 'generated/l10n.dart';

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            localizationsDelegates: [
                S.delegate, //The class S
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: S.delegate.supportedLocales,
            title: 'Flutter Demo',
            home: MyHomePage(),
        );
    }
}

Now you can use it down in the Widget tree

class MyHomePage extends StatelessWidget{
   @override
   Widget build(BuildContext context){
     return Text(S.of(context).name) 
     // now it prints the string of name depending the language the device is
   }
}

If the device is in a language you don't support (for ex French in my case) it will use the default language of the class, en ('English), check main_locale in the pubspec if you want to use another as your default

Check this Example of how to use internationalization with JSON keys if you want to try something different.




回答2:


You ll probably find everything you searching for in the Internationalizing Flutter Apps doc !



来源:https://stackoverflow.com/questions/62433208/flutter-how-to-detect-device-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!