slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
419 stars 36 forks source link

When and where should I use setPluralResolver? #223

Closed sed1ka closed 2 weeks ago

sed1ka commented 3 weeks ago

Thanks for great localizations library

I got an exception message Resolver for <lang = id> not specified! Please configure it via LocaleSettings.setPluralResolver. A fallback is used now.

The explanation in #167 is not clear enough for me.

So where and when should I use setPluralResolver?

Currently, I only used t.<localtizationsKey> directly

class ExampleView extends StatelessWidget {
  const ExampleView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(t.name),
      ),
    );
  }
}

and with final t = AppLocalizations.of(context); in build method,

class ExampleView extends StatelessWidget {
  const ExampleView({super.key});

  @override
  Widget build(BuildContext context) {
  final t = AppLocalizations.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(t.name),
      ),
    );
  }
}

Based on #167, do you mean I need to always call setPluralResolver after using any LocaleSettings? Example like:

// Set the local
LocaleSettings.setLocale(_);
// Then I need to set the plural resolver after
LocaleSettings.setPluralResolver(_);

Or can I ignore the exception warning? because _defaultResolver on plural_resolver_map.dart was enough for me.

If my assumptions are wrong, please tell me correctly when and where I should use setPluralResolver. Please provide examples to be very helpful. Thanks

Tienisto commented 3 weeks ago

Hello, you can call LocaleSettings.setPluralResolver anywhere before accessing a translation. For example, I am initializing it before creating the MaterialApp.

For example

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  LocaleSettings.setPluralResolver(...); // before any translation access is good enough

  LocaleSettings.useDeviceLocale();

  runApp(TranslationProvider(
    child: MyApp(),
  ));
}
sed1ka commented 2 weeks ago

Thanks for the quick response. But after looking deeply, I think it's fine to ignore the exception at least the _defaultResolver on plural_resolver_map.dart is enough for my case.