Flutter Dart-从国家代码获取本地国家名称

发布于 2025-02-12 13:15:03 字数 452 浏览 0 评论 0原文

我有Alpha-2国家 /地区代码的列表。

 List<String> countryCodes = ["DE", "CH", "AT"];

如何在Flutter/Dart中获取本地化的国家名称?

如果设备语言设置为英语,我想要此输出。

德国,瑞士,奥地利

如果设备语言设置为德语,我想要此输出。

Deutschland,Schweiz,Österreich

如果没有Flutter/Dart中的任何第三方套餐,这是否有可能?

在Java,您可以做..

Locale loc = new Locale("","DE");
loc.getDisplayCountry();

I have a list of alpha-2 country codes.

 List<String> countryCodes = ["DE", "CH", "AT"];

How to get the localized country names in Flutter/Dart?

If the device language is set to english, i want this output..

Germany, Switzerland, Austria

If the device language is set to german, i want this output..

Deutschland, Schweiz, Österreich

Is this possible without any 3rd party package in Flutter/Dart?

In Java you could do..

Locale loc = new Locale("","DE");
loc.getDisplayCountry();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2025-02-19 13:15:04

您可以使用 country_picker

MaterialApp(
  supportedLocales: [
    const Locale('en'),
    const Locale('lt'),
  ],
  localizationsDelegates: [
    CountryLocalizations.delegate,
    ...
  ],
  ...)

然后通过:

CountryLocalizations.of(context)?.countryName(countryCode: 'LT')

You can use country_picker :

MaterialApp(
  supportedLocales: [
    const Locale('en'),
    const Locale('lt'),
  ],
  localizationsDelegates: [
    CountryLocalizations.delegate,
    ...
  ],
  ...)

and then get the localized country name via :

CountryLocalizations.of(context)?.countryName(countryCode: 'LT')
救赎№ 2025-02-19 13:15:03

为了从一个国家代码中获取本地化的国家名称,可以使用pub.dev的国家代码包。

它易于使用。我建议。

https://pub.dev/packages/country_codes

To get the localized country names from a country code one can use the Country Codes package from pub.dev.

Its easy to use. I recommend.

https://pub.dev/packages/country_codes

z祗昰~ 2025-02-19 13:15:03

如果您有任何国家代码并且想要获得相应的国家名称,请使用 country_picker 像这样的包装

Country.tryParse(countryCode)?.name

If you have any country code and wants to get corresponding country name, use country_picker package like this

Country.tryParse(countryCode)?.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文