Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localizations doesn't work #22

Closed
tverra opened this issue Mar 19, 2021 · 2 comments
Closed

Localizations doesn't work #22

tverra opened this issue Mar 19, 2021 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@tverra
Copy link
Contributor

tverra commented Mar 19, 2021

Hi. I've made an app that uses localizations, as described in the Flutter docs. I've tried to run the example project, and that implementation works. However, when implemented using the official Flutter guide, it doesn't work. I've added a complete runnable project that demonstrates this issue.

Other localized strings works as intended , but the static CountryLocalizations? of(BuildContext context) method in country_localizations.dart returns null, which means that none of the strings in the package are translated. I'm not completely sure what causes this issue, because the inner workings of the Flutter localizations still remains a mystery to me.

An example of a workaround fould be to return CountryLocalizations(Localizations.localeOf(context)) instead of Localizations.of<CountryLocalizations>(context, CountryLocalizations), but I don't know if this comes with some kind of cost. Thoughts?

@Daniel-Ioannou
Copy link
Owner

@tverra The package follows the official Flutter guide for localizations.
For each package that support localization the app must include the package's delegate in the list of the app delegates. So for country_picker package you have to add the CountryLocalizations.delegate in the list of your app delegates.

If you check the example from the Flutter docs, in this example the app import localizations for Material, Widgets, Cupertino and the app.

MaterialApp(
localizationsDelegates: [
  AppLocalizations.delegate,
  GlobalMaterialLocalizations.delegate,
  GlobalWidgetsLocalizations.delegate,
  GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
   const Locale('en', ''), // English, no country code
   const Locale('ar', ''), // Arabic, no country code
   const Locale.fromSubtags(languageCode: 'zh'), // Chinese *See Advanced Locales below*
 ],
)

So in your example project change the code to:

MaterialApp(
 title: 'Localizations Demo',
 localizationsDelegates: [
   CountryLocalizations.delegate,
   ...AppLocalizations.localizationsDelegates
 ],
 supportedLocales: AppLocalizations.supportedLocales,
 theme: ThemeData(
   primarySwatch: Colors.blue,
 ),
 home: MyHomePage(),
);

@Daniel-Ioannou Daniel-Ioannou added the documentation Improvements or additions to documentation label Mar 20, 2021
@tverra
Copy link
Contributor Author

tverra commented Mar 22, 2021

You're right, I completely missed that. I haven't seen this approach in a Flutter package before. I'm using this package inside my own package, and it kinda seems like just getting the locale from the application context is a more elegant solution than needing the user of my package to add localization delegates for every translatable package their application depends on. But anyway, thanks!

@tverra tverra closed this as completed Mar 22, 2021
Repository owner locked and limited conversation to collaborators Aug 31, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants