Closed guoyutao closed 2 years ago
有示例代码吗?
Hi Thanks for the question!
For now the third party plugin flutter_localizations
is not ported to UIWidgets yet. As the result, currently we don't support its relevant functionalities.
However, as a quick work-around, you can directly change the value of pasteButtonLabel
, cutButtonLabel
, etc. inside the material/material_localizations.cs
file to make them Chinese
Hi Thanks for the question!
For now the third party plugin
flutter_localizations
is not ported to UIWidgets yet. As the result, currently we don't support its relevant functionalities.However, as a quick work-around, you can directly change the value of
pasteButtonLabel
,cutButtonLabel
, etc. inside thematerial/material_localizations.cs
file to make them Chinese
谢谢
@zhuxingwei 你好,我看之前您在1.5版本写过一个语言国际化的demo,2.0可以也写一个么?
Hi Thanks for the reply!
Could you please share me a link of this localization demo you mentioned and I will have a look then. Thanks !
public class DemoLocalization : WidgetsLocalizations
{
private readonly Locale locale;
public DemoLocalization(Locale locale)
{
this.locale = locale;
}
static readonly Dictionary<string, Dictionary<string, string>> _localizedValues = new Dictionary<string, Dictionary<string, string>>
{
{"en", new Dictionary<string, string>
{
{"task title", "flutter demo"}
}},
{"zh", new Dictionary<string, string>
{
{"task title", "flutter 例子"}
}}
};
public string title => _localizedValues[locale.languageCode]["task title"];
public static DemoLocalization of(BuildContext context){
return Localizations.of<DemoLocalization>(context, typeof(DemoLocalization));
}
}
public class DemoLocalizationsDelegate : LocalizationsDelegate<DemoLocalization>
{
public override bool isSupported(Locale locale)
{
return locale.languageCode == "en" || locale.languageCode == "zh";
}
public override Future<WidgetsLocalizations> load(Locale locale)
{
return new SynchronousFuture<WidgetsLocalizations>(new DemoLocalization(locale));
}
public override bool shouldReload(LocalizationsDelegate old)
{
return false;
}
public static readonly DemoLocalizationsDelegate DemoDelegate = new DemoLocalizationsDelegate();
}
public class DefaultMaterialLocalizationsCN : DefaultMaterialLocalizations
{
public static readonly _MaterialLocalizationsDelegateCN DemoDelegate = new _MaterialLocalizationsDelegateCN();
}
public class _MaterialLocalizationsDelegateCN : LocalizationsDelegate<MaterialLocalizations> {
public _MaterialLocalizationsDelegateCN() {
}
public override bool isSupported(Locale locale) {
return locale.languageCode == "en" || locale.languageCode == "zh";
}
public override Future<WidgetsLocalizations> load(Locale locale) {
return DefaultMaterialLocalizations.load(locale).to<WidgetsLocalizations>();
}
public override bool shouldReload(LocalizationsDelegate old) {
return false;
}
public override string ToString() {
return "DefaultMaterialLocalizations.delegate(en_US)";
}
}
public class MyApp3 : StatelessWidget
{
public override Widget build(BuildContext context)
{
return new Scaffold(
appBar: new AppBar(title: new Text("Title")),
body: new Column(
children: new List<Widget>{
new Text(DemoLocalization.of(context).title),
new ConstrainedBox(
constraints: new BoxConstraints(
maxHeight: 120.0f
),
child: new TextField(
maxLines: 4,
minLines: 1,
maxLengthEnforced: false,
keyboardType: TextInputType.multiline
)
),
new Container(height: 20.0f,width: 200.0f,color: Colors.red)
}
)
);
}
}
public class MyApp2 : StatelessWidget
{
public override Widget build(BuildContext context)
{
return new MaterialApp(
title: "flutter demo",
home: new MyApp3(),
localizationsDelegates: new List<LocalizationsDelegate>
{
DefaultMaterialLocalizationsCN.DemoDelegate,
DemoLocalizationsDelegate.DemoDelegate,
},
locale: new Locale("en", "US"), // this is for Chinese, set as new Locale("en", "US") for English
supportedLocales: new List<Locale>
{
new Locale("zh", "CN"),
new Locale("en", "US")
}
);
}
}
@zhuxingwei 谢谢