Files
patbond-flutter/lib/app/app_localization.dart
T
lixi 6038901099 修复:Material 内置组件中文化——挂 zh-CN 三件套 delegate(M3.5-01)
用户 Linux 桌面实测反馈「日期选择器全英文」。根因是 MaterialApp 从一迭代
建起就没配 localizationsDelegates/supportedLocales/locale,pubspec 也没有
flutter_localizations;Flutter 在缺 delegate 时静默回退内置的
DefaultMaterialLocalizations(只有英文),于是业务自绘文案全中文、Material
内置组件全英文,同一弹窗里中英混排:

  Select date → 选择日期
  OK / Cancel → 确定 / 取消
  Enter Date → 输入日期
  Switch to input → 切换到输入模式
  Invalid format. / Out of range. → 格式无效。/ 超出范围。
  mm/dd/yyyy → yyyy/mm/dd(zh 顺序年在前)
  September 2026 → 2026年9月

处置:

- pubspec 加 flutter_localizations(sdk)与 intl(日期符号底座,显式直接
  依赖以锁定与 SDK 一致的版本)。
- 新增 lib/app/app_localization.dart 把 Global{Material,Cupertino,Widgets}
  三件套 delegate、supportedLocales、locale 收在一处常量:widget 测试若只
  pumpWidget(MaterialApp(home: ...)) 而不挂 delegate,测到的「中文」是假的,
  测试直接引用同一份常量,生产与测试不会各配一套而漂移。
- 单语言 zh-CN,不列 en:避免设备语言为英文时回退英文,再造混排。
- pets 域三份 widget 测试的 MaterialApp 同步挂上 delegate,
  tap(find.text('OK')) 相应改 '确定'——让测试与生产一致,而不是在英文兜底下测。

新增 test/app/app_localization_test.dart 守住这一行不被回删:断言根
MaterialApp 实际挂上三件套 + locale zh-CN,并从运行期 MaterialLocalizations
取回中文文案。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-10 17:59:21 +08:00

32 lines
1.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 应用本地化配置(M3.5-01)。
///
/// 根因备忘:M3 之前 [MaterialApp] 未配任何 `localizationsDelegates`
/// Flutter 回退到内置的 `DefaultMaterialLocalizations`(仅英文),导致
/// 一切 Material 内置组件(日期选择器标题「Select date」、确定/取消
/// 「OK」/「Cancel」、模式切换 tooltip「Switch to input」、格式报错
/// 「Invalid format.」)全英文,而业务自绘文案全中文——同一弹窗内中英混排。
/// 挂上三件套后分别为「选择日期 / 确定 / 取消 / 切换到输入模式 / 格式无效。」。
///
/// 这里把三件套 delegate 与 locale 收在一处,供 `App` 与 widget 测试共用:
/// 测试若只 `pumpWidget(MaterialApp(home: ...))` 而不带 delegate,看到的仍是
/// 英文兜底,与真机不一致;需要断言中文渲染的测试请一并挂上本文件的常量。
library;
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
/// Material / Cupertino / Widgets 三件套本地化 delegate。
const List<LocalizationsDelegate<Object>> appLocalizationsDelegates =
<LocalizationsDelegate<Object>>[
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
];
/// 简体中文(中国大陆)。M3.5 仅单语言,暂不做语言切换。
const Locale appLocale = Locale('zh', 'CN');
/// 支持语言列表。仅 zh-CN——避免设备语言为英文时回退到英文,
/// 造成同一界面内业务中文 + 组件英文的混排。
const List<Locale> appSupportedLocales = <Locale>[appLocale];