Skip to content

国际化 ​

语言包 ​

简单的映射

dart
class Messages extends Translations {
  @override
  Map<String, Map<String, String>> get keys => {
    'zh_CN': zh,
    'en_US': en,
  };
}

Keys

dart
class MsgKeys {
  static const hello = 'hello';
  static const world = 'wrold';
}

Values

dart
const zh = {
  MsgKeys.hello: '你好';
  MsgKeys.world: '世界';
}

const en = {
  MsgKeys.hello: 'Hello';
  MsgKeys.world: 'World';
}

国际化配置 ​

dart
GetMaterialApp(
  translations: Messages(),
  locale: const Locale('en', 'US'),
  fallbackLocale: const Locale('en', 'US'),
);

// 更改
var locale = Locale('zh', 'CN');
Get.updateLocale(locale);

使用 ​

dart
Text(MsgKeys.hello.tr);
Text(MsgKeys.world.tr);