Flutter工具类(五) 国际化

导入依赖

#国际化
  intl: ^0.17.0

工具类

messages_all.dart


import 'dart:async';

import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';
import 'package:intl/src/intl_helpers.dart';

import 'messages_en.dart' as messages_en;
import 'messages_zh.dart' as messages_zh;

typedef Future<dynamic> LibraryLoader();
Map<String, LibraryLoader> _deferredLibraries = {
  'en': () => new Future.value(null),
  'zh': () => new Future.value(null),
};

MessageLookupByLibrary? _findExact(String localeName) {
  switch (localeName) {
    case 'en':
      return messages_en.messages;
    case 'zh':
      return messages_zh.messages;
    default:
      return null;
  }
}

/// User programs should call this before using [localeName] for messages.
Future<bool> initializeMessages(String localeName) async {
  var availableLocale = Intl.verifiedLocale(
      localeName, (locale) => _deferredLibraries[locale] != null,
      onFailure: (_) => null);
  if (availableLocale == null) {
    return new Future.value(false);
  }
  var lib = _deferredLibraries[availableLocale];
  await (lib == null ? new Future.value(false) : lib());
  initializeInternalMessageLookup(() => new CompositeMessageLookup());
  messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
  return new Future.value(true);
}

bool _messagesExistFor(String locale) {
  try {
    return _findExact(locale) != null;
  } catch (e) {
    return false;
  }
}

MessageLookupByLibrary? _findGeneratedMessagesFor(String locale) {
  var actualLocale =
      Intl.verifiedLocale(locale, _messagesExistFor, onFailure: (_) => null);
  if (actualLocale == null) return null;
  return _findExact(actualLocale);
}

messages_en.dart

// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
// This is a library that provides messages for a en locale. All the
// messages from the main program should be duplicated here with the same
// function name.

// Ignore issues from commonly used lints in this file.
// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes

import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';

final messages = new MessageLookup();

typedef String MessageIfAbsent(String messageStr, List<dynamic> args);

class MessageLookup extends MessageLookupByLibrary {
  String get localeName => 'en';

  static String m0(value) => "Integral:${value}";

  static String m1(main, sub) => "${main} - ${sub}";

  static String m2(progress) => "Learned${progress}%";

  final messages = _notInlinedMessages(_notInlinedMessages);
  static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
        "account_empty_tip":
            MessageLookupByLibrary.simpleMessage("Please enter an account"),
        "cancel": MessageLookupByLibrary.simpleMessage("Cancel"),
        "clean_all": MessageLookupByLibrary.simpleMessage("Clear all"),
        "collect": MessageLookupByLibrary.simpleMessage("Collect"),
        "collect_content": MessageLookupByLibrary.simpleMessage(
            "Are you sure remove the collected content?"),
        "color_theme": MessageLookupByLibrary.simpleMessage("Color Theme"),
        "confirm": MessageLookupByLibrary.simpleMessage("Confirm"),
        "dark_style": MessageLookupByLibrary.simpleMessage("Dark Mode"),
        "done": MessageLookupByLibrary.simpleMessage("Done"),
        "edit": MessageLookupByLibrary.simpleMessage("Edit"),
        "integral": m0,
        "label_group": m1,
        "language_chinese": MessageLookupByLibrary.simpleMessage("Chinese"),
        "language_english": MessageLookupByLibrary.simpleMessage("English"),
        "learn_no": MessageLookupByLibrary.simpleMessage("No learned"),
        "learn_progress": m2,
        "loading_content":
            MessageLookupByLibrary.simpleMessage("content loading..."),
        "login": MessageLookupByLibrary.simpleMessage("Login"),
        "login_success": MessageLookupByLibrary.simpleMessage("Login success"),
        "multi_language":
            MessageLookupByLibrary.simpleMessage("Multi language"),
        "net_error": MessageLookupByLibrary.simpleMessage("Network error"),
        "no_account": MessageLookupByLibrary.simpleMessage("No account yet?"),
        "placeholder": MessageLookupByLibrary.simpleMessage("--"),
        "psw_confirm_empty_tip":
            MessageLookupByLibrary.simpleMessage("Please confirm the password"),
        "psw_confirm_tip": MessageLookupByLibrary.simpleMessage(
            "Two passwords are inconsistent"),
        "psw_empty_tip":
            MessageLookupByLibrary.simpleMessage("Please enter an password"),
        "register": MessageLookupByLibrary.simpleMessage("Register"),
        "register_now": MessageLookupByLibrary.simpleMessage("Register now"),
        "register_success":
            MessageLookupByLibrary.simpleMessage("Register success"),
        "search_hint": MessageLookupByLibrary.simpleMessage(
            "separate multiple keywords with spaces"),
        "search_hot_title":
            MessageLookupByLibrary.simpleMessage("Popular search"),
        "search_local_title":
            MessageLookupByLibrary.simpleMessage("Historical search"),
        "settings": MessageLookupByLibrary.simpleMessage("Settings"),
        "tab_book": MessageLookupByLibrary.simpleMessage("Book"),
        "tab_book_course":
            MessageLookupByLibrary.simpleMessage("Book tutorial"),
        "tab_home": MessageLookupByLibrary.simpleMessage("Home"),
        "tab_knowledge": MessageLookupByLibrary.simpleMessage("Knowledge"),
        "tab_me": MessageLookupByLibrary.simpleMessage("Me"),
        "tab_nav": MessageLookupByLibrary.simpleMessage("Navigation"),
        "tab_project": MessageLookupByLibrary.simpleMessage("Project"),
        "tab_tree": MessageLookupByLibrary.simpleMessage("System"),
        "tips_msg": MessageLookupByLibrary.simpleMessage("Tips"),
        "topping": MessageLookupByLibrary.simpleMessage("Topping"),
        "user_name": MessageLookupByLibrary.simpleMessage("UserName"),
        "user_psw": MessageLookupByLibrary.simpleMessage("Password"),
        "user_psw_confirm":
            MessageLookupByLibrary.simpleMessage("Confirm Password")
      };
}

messages_zh.dart

// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart
// This is a library that provides messages for a zh locale. All the
// messages from the main program should be duplicated here with the same
// function name.

// Ignore issues from commonly used lints in this file.
// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new
// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering
// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases
// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes
// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes

import 'package:intl/intl.dart';
import 'package:intl/message_lookup_by_library.dart';

final messages = new MessageLookup();

typedef String MessageIfAbsent(String messageStr, List<dynamic> args);

class MessageLookup extends MessageLookupByLibrary {
  String get localeName => 'zh';

  static String m0(value) => "积分:${value}";

  static String m1(main, sub) => "${main} - ${sub}";

  static String m2(progress) => "已学${progress}%";

  final messages = _notInlinedMessages(_notInlinedMessages);
  static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
        "account_empty_tip": MessageLookupByLibrary.simpleMessage("请输入账号"),
        "cancel": MessageLookupByLibrary.simpleMessage("取消"),
        "clean_all": MessageLookupByLibrary.simpleMessage("清除全部"),
        "collect": MessageLookupByLibrary.simpleMessage("收藏"),
        "collect_content": MessageLookupByLibrary.simpleMessage("您确定要移除收藏内容吗?"),
        "color_theme": MessageLookupByLibrary.simpleMessage("彩色主题"),
        "confirm": MessageLookupByLibrary.simpleMessage("确定"),
        "dark_style": MessageLookupByLibrary.simpleMessage("暗黑模式"),
        "done": MessageLookupByLibrary.simpleMessage("完成"),
        "edit": MessageLookupByLibrary.simpleMessage("编辑"),
        "integral": m0,
        "label_group": m1,
        "language_chinese": MessageLookupByLibrary.simpleMessage("中文"),
        "language_english": MessageLookupByLibrary.simpleMessage("英文"),
        "learn_no": MessageLookupByLibrary.simpleMessage("未学习"),
        "learn_progress": m2,
        "loading_content": MessageLookupByLibrary.simpleMessage("内容加载中..."),
        "login": MessageLookupByLibrary.simpleMessage("登录"),
        "login_success": MessageLookupByLibrary.simpleMessage("登录成功"),
        "multi_language": MessageLookupByLibrary.simpleMessage("多语言"),
        "net_error": MessageLookupByLibrary.simpleMessage("网络错误"),
        "no_account": MessageLookupByLibrary.simpleMessage("还没账号?"),
        "placeholder": MessageLookupByLibrary.simpleMessage("--"),
        "psw_confirm_empty_tip": MessageLookupByLibrary.simpleMessage("请确认密码"),
        "psw_confirm_tip": MessageLookupByLibrary.simpleMessage("两次密码不一致"),
        "psw_empty_tip": MessageLookupByLibrary.simpleMessage("请输入密码"),
        "register": MessageLookupByLibrary.simpleMessage("注册"),
        "register_now": MessageLookupByLibrary.simpleMessage("立即注册"),
        "register_success": MessageLookupByLibrary.simpleMessage("注册成功"),
        "search_hint": MessageLookupByLibrary.simpleMessage("用空格分隔多个关键词"),
        "search_hot_title": MessageLookupByLibrary.simpleMessage("热门搜索"),
        "search_local_title": MessageLookupByLibrary.simpleMessage("历史搜索"),
        "settings": MessageLookupByLibrary.simpleMessage("设置"),
        "tab_book": MessageLookupByLibrary.simpleMessage("教程"),
        "tab_book_course": MessageLookupByLibrary.simpleMessage("书籍教程"),
        "tab_home": MessageLookupByLibrary.simpleMessage("首页"),
        "tab_knowledge": MessageLookupByLibrary.simpleMessage("知识"),
        "tab_me": MessageLookupByLibrary.simpleMessage("我的"),
        "tab_nav": MessageLookupByLibrary.simpleMessage("导航"),
        "tab_project": MessageLookupByLibrary.simpleMessage("项目"),
        "tab_tree": MessageLookupByLibrary.simpleMessage("体系"),
        "tips_msg": MessageLookupByLibrary.simpleMessage("提示"),
        "topping": MessageLookupByLibrary.simpleMessage("置顶"),
        "user_name": MessageLookupByLibrary.simpleMessage("用户名"),
        "user_psw": MessageLookupByLibrary.simpleMessage("密码"),
        "user_psw_confirm": MessageLookupByLibrary.simpleMessage("确认密码")
      };
}

l10n.dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'intl/messages_all.dart';

// **************************************************************************
// Generator: Flutter Intl IDE plugin
// Made by Localizely
// **************************************************************************

// ignore_for_file: non_constant_identifier_names, lines_longer_than_80_chars
// ignore_for_file: join_return_with_assignment, prefer_final_in_for_each
// ignore_for_file: avoid_redundant_argument_values, avoid_escaping_inner_quotes

class S {
  S();

  static S? _current;

  static S get current {
    assert(_current != null,
        'No instance of S was loaded. Try to initialize the S delegate before accessing S.current.');
    return _current!;
  }

  static const AppLocalizationDelegate delegate = AppLocalizationDelegate();

  static Future<S> load(Locale locale) {
    final name = (locale.countryCode?.isEmpty ?? false)
        ? locale.languageCode
        : locale.toString();
    final localeName = Intl.canonicalizedLocale(name);
    return initializeMessages(localeName).then((_) {
      Intl.defaultLocale = localeName;
      final instance = S();
      S._current = instance;

      return instance;
    });
  }

  static S of(BuildContext context) {
    final instance = S.maybeOf(context);
    assert(instance != null,
        'No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?');
    return instance!;
  }

  static S? maybeOf(BuildContext context) {
    return Localizations.of<S>(context, S);
  }

  /// `Home`
  String get tab_home {
    return Intl.message(
      'Home',
      name: 'tab_home',
      desc: '',
      args: [],
    );
  }

  /// `Project`
  String get tab_project {
    return Intl.message(
      'Project',
      name: 'tab_project',
      desc: '',
      args: [],
    );
  }

  /// `Knowledge`
  String get tab_knowledge {
    return Intl.message(
      'Knowledge',
      name: 'tab_knowledge',
      desc: '',
      args: [],
    );
  }

  /// `Book`
  String get tab_book {
    return Intl.message(
      'Book',
      name: 'tab_book',
      desc: '',
      args: [],
    );
  }

  /// `Me`
  String get tab_me {
    return Intl.message(
      'Me',
      name: 'tab_me',
      desc: '',
      args: [],
    );
  }

  /// `Integral:{value}`
  String integral(Object value) {
    return Intl.message(
      'Integral:$value',
      name: 'integral',
      desc: '',
      args: [value],
    );
  }

  /// `Collect`
  String get collect {
    return Intl.message(
      'Collect',
      name: 'collect',
      desc: '',
      args: [],
    );
  }

  /// `Dark Mode`
  String get dark_style {
    return Intl.message(
      'Dark Mode',
      name: 'dark_style',
      desc: '',
      args: [],
    );
  }

  /// `Color Theme`
  String get color_theme {
    return Intl.message(
      'Color Theme',
      name: 'color_theme',
      desc: '',
      args: [],
    );
  }

  /// `Settings`
  String get settings {
    return Intl.message(
      'Settings',
      name: 'settings',
      desc: '',
      args: [],
    );
  }

  /// `--`
  String get placeholder {
    return Intl.message(
      '--',
      name: 'placeholder',
      desc: '',
      args: [],
    );
  }

  /// `Login`
  String get login {
    return Intl.message(
      'Login',
      name: 'login',
      desc: '',
      args: [],
    );
  }

  /// `Register`
  String get register {
    return Intl.message(
      'Register',
      name: 'register',
      desc: '',
      args: [],
    );
  }

  /// `Register now`
  String get register_now {
    return Intl.message(
      'Register now',
      name: 'register_now',
      desc: '',
      args: [],
    );
  }

  /// `No account yet?`
  String get no_account {
    return Intl.message(
      'No account yet?',
      name: 'no_account',
      desc: '',
      args: [],
    );
  }

  /// `UserName`
  String get user_name {
    return Intl.message(
      'UserName',
      name: 'user_name',
      desc: '',
      args: [],
    );
  }

  /// `Password`
  String get user_psw {
    return Intl.message(
      'Password',
      name: 'user_psw',
      desc: '',
      args: [],
    );
  }

  /// `Confirm Password`
  String get user_psw_confirm {
    return Intl.message(
      'Confirm Password',
      name: 'user_psw_confirm',
      desc: '',
      args: [],
    );
  }

  /// `Topping`
  String get topping {
    return Intl.message(
      'Topping',
      name: 'topping',
      desc: '',
      args: [],
    );
  }

  /// `{main} - {sub}`
  String label_group(Object main, Object sub) {
    return Intl.message(
      '$main - $sub',
      name: 'label_group',
      desc: '',
      args: [main, sub],
    );
  }

  /// `separate multiple keywords with spaces`
  String get search_hint {
    return Intl.message(
      'separate multiple keywords with spaces',
      name: 'search_hint',
      desc: '',
      args: [],
    );
  }

  /// `Popular search`
  String get search_hot_title {
    return Intl.message(
      'Popular search',
      name: 'search_hot_title',
      desc: '',
      args: [],
    );
  }

  /// `Historical search`
  String get search_local_title {
    return Intl.message(
      'Historical search',
      name: 'search_local_title',
      desc: '',
      args: [],
    );
  }

  /// `Edit`
  String get edit {
    return Intl.message(
      'Edit',
      name: 'edit',
      desc: '',
      args: [],
    );
  }

  /// `Clear all`
  String get clean_all {
    return Intl.message(
      'Clear all',
      name: 'clean_all',
      desc: '',
      args: [],
    );
  }

  /// `Done`
  String get done {
    return Intl.message(
      'Done',
      name: 'done',
      desc: '',
      args: [],
    );
  }

  /// `content loading...`
  String get loading_content {
    return Intl.message(
      'content loading...',
      name: 'loading_content',
      desc: '',
      args: [],
    );
  }

  /// `Learned{progress}%`
  String learn_progress(Object progress) {
    return Intl.message(
      'Learned$progress%',
      name: 'learn_progress',
      desc: '',
      args: [progress],
    );
  }

  /// `No learned`
  String get learn_no {
    return Intl.message(
      'No learned',
      name: 'learn_no',
      desc: '',
      args: [],
    );
  }

  /// `Multi language`
  String get multi_language {
    return Intl.message(
      'Multi language',
      name: 'multi_language',
      desc: '',
      args: [],
    );
  }

  /// `Chinese`
  String get language_chinese {
    return Intl.message(
      'Chinese',
      name: 'language_chinese',
      desc: '',
      args: [],
    );
  }

  /// `English`
  String get language_english {
    return Intl.message(
      'English',
      name: 'language_english',
      desc: '',
      args: [],
    );
  }

  /// `Please enter an account`
  String get account_empty_tip {
    return Intl.message(
      'Please enter an account',
      name: 'account_empty_tip',
      desc: '',
      args: [],
    );
  }

  /// `Please enter an password`
  String get psw_empty_tip {
    return Intl.message(
      'Please enter an password',
      name: 'psw_empty_tip',
      desc: '',
      args: [],
    );
  }

  /// `Please confirm the password`
  String get psw_confirm_empty_tip {
    return Intl.message(
      'Please confirm the password',
      name: 'psw_confirm_empty_tip',
      desc: '',
      args: [],
    );
  }

  /// `Two passwords are inconsistent`
  String get psw_confirm_tip {
    return Intl.message(
      'Two passwords are inconsistent',
      name: 'psw_confirm_tip',
      desc: '',
      args: [],
    );
  }

  /// `Login success`
  String get login_success {
    return Intl.message(
      'Login success',
      name: 'login_success',
      desc: '',
      args: [],
    );
  }

  /// `Register success`
  String get register_success {
    return Intl.message(
      'Register success',
      name: 'register_success',
      desc: '',
      args: [],
    );
  }

  /// `Network error`
  String get net_error {
    return Intl.message(
      'Network error',
      name: 'net_error',
      desc: '',
      args: [],
    );
  }

  /// `System`
  String get tab_tree {
    return Intl.message(
      'System',
      name: 'tab_tree',
      desc: '',
      args: [],
    );
  }

  /// `Navigation`
  String get tab_nav {
    return Intl.message(
      'Navigation',
      name: 'tab_nav',
      desc: '',
      args: [],
    );
  }

  /// `Book tutorial`
  String get tab_book_course {
    return Intl.message(
      'Book tutorial',
      name: 'tab_book_course',
      desc: '',
      args: [],
    );
  }

  /// `Tips`
  String get tips_msg {
    return Intl.message(
      'Tips',
      name: 'tips_msg',
      desc: '',
      args: [],
    );
  }

  /// `Cancel`
  String get cancel {
    return Intl.message(
      'Cancel',
      name: 'cancel',
      desc: '',
      args: [],
    );
  }

  /// `Confirm`
  String get confirm {
    return Intl.message(
      'Confirm',
      name: 'confirm',
      desc: '',
      args: [],
    );
  }

  /// `Are you sure remove the collected content?`
  String get collect_content {
    return Intl.message(
      'Are you sure remove the collected content?',
      name: 'collect_content',
      desc: '',
      args: [],
    );
  }
}

class AppLocalizationDelegate extends LocalizationsDelegate<S> {
  const AppLocalizationDelegate();

  List<Locale> get supportedLocales {
    return const <Locale>[
      Locale.fromSubtags(languageCode: 'en'),
      Locale.fromSubtags(languageCode: 'zh'),
    ];
  }

  @override
  bool isSupported(Locale locale) => _isSupported(locale);
  @override
  Future<S> load(Locale locale) => S.load(locale);
  @override
  bool shouldReload(AppLocalizationDelegate old) => false;

  bool _isSupported(Locale locale) {
    for (var supportedLocale in supportedLocales) {
      if (supportedLocale.languageCode == locale.languageCode) {
        return true;
      }
    }
    return false;
  }
}

使用案列
main.dart

return MaterialApp(
        
            /// widget本地化代理设置
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              S.delegate //自定义国际化
            ],
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值