Unity-Technologies / com.unity.uiwidgets

UIWidgets is a Unity Package which helps developers to create, debug and deploy efficient, cross-platform Apps.
https://unity.cn/uiwidgets
626 stars 78 forks source link

flutter 中的中文设置 如何再UIWidgets中设置呢? #339

Closed guoyutao closed 2 years ago

guoyutao commented 2 years ago
image
guoyutao commented 2 years ago

有示例代码吗?

zhuxingwei commented 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

guoyutao commented 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

谢谢

xuexirong commented 2 years ago

@zhuxingwei 你好,我看之前您在1.5版本写过一个语言国际化的demo,2.0可以也写一个么?

zhuxingwei commented 2 years ago

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 !

xuexirong commented 2 years ago

@zhuxingwei https://github.com/UnityTech/UIWidgets/issues/397

zhuxingwei commented 2 years ago
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")
            }
        );
    }
}
xuexirong commented 2 years ago

@zhuxingwei 谢谢