Cannot translate language in Flutter

255 Views Asked by At

I'm using easy_localization in my app. I have changed the language setting from English to Chinese, but app still displays English.

This is my project directory:

enter image description here

main.dart

 runApp(EasyLocalization(
      supportedLocales: const [Locale('en', 'US'), Locale('zh-CN', 'CN')],
      path: 'assets/translations',
      child: const App(),
    )

zh-CN.json

"hello": "你好",

widget.dart

Text('hello'.tr());

Is it because the languageCode and countryCode wrong?

2

There are 2 best solutions below

4
Javatar On
Create folder and add translation files like this

assets
└── translations
    ├── {languageCode}.{ext}                  //only language code
    └── {languageCode}-{countryCode}.{ext}    //or full locale code

Example:

assets
└── translations
    ├── en.json
    └── en-US.json 

Declare your assets localization directory in pubspec.yaml:

flutter:
  assets:
    - assets/translations/
0
Rohit Singla On

From the documentation of Locale(this._languageCode, [this._countryCode,]), the value 'zh-CN' is not supported for the parameter _languageCode.

The subtag values are case sensitive and must be one of the valid subtags according to CLDR supplemental data:
language,
region.

########## Solution #########
Try replacing:-

supportedLocales: const [Locale('en', 'US'), Locale('zh-CN', 'CN')],

with

supportedLocales: [Locale('en', 'US'), Locale('zh', 'CN')],

If this is not meeting your requirements, you can consider looking for an alternate language code which is supported from this link : language

Hope this solves your problem.