Skip to content
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

Use with Flutter Intl localization #97

Open
avvari-da opened this issue Apr 11, 2020 · 0 comments
Open

Use with Flutter Intl localization #97

avvari-da opened this issue Apr 11, 2020 · 0 comments

Comments

@avvari-da
Copy link

avvari-da commented Apr 11, 2020

Thanks for this great package.

How can make it work with Flutter's Intl package?

I am trying to give the manual toggle in the app for language change, and want the locale with this package to auto update.
Please help!

Code of my localization dart files:

app_localization.dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:praja/l10n/messages_all.dart';

class AppLocalization {
  static String localeName = "en";

  static Future<AppLocalization> load(Locale locale) {
    final String name =
        locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
    localeName = Intl.canonicalizedLocale(name);
    return initializeMessages(localeName).then((_) {
      Intl.defaultLocale = localeName;
      return AppLocalization();
    });
  }

  static AppLocalization of(BuildContext context) {
    return Localizations.of<AppLocalization>(context, AppLocalization);
  }

  // list of locales
  String get heyWorld {
    return Intl.message(
      'Hey World',
      name: 'heyWorld',
      desc: 'Simple word for greeting ',
    );
  }
}

app_localization_delegate.dart

import 'package:flutter/material.dart';
import 'package:praja/locale/app_localization.dart';

class AppLocalizationDelegate extends LocalizationsDelegate<AppLocalization> {
  final Locale overriddenLocale;

  const AppLocalizationDelegate(this.overriddenLocale);

  @override
  bool isSupported(Locale locale) => ['en', 'te'].contains(locale.languageCode);

  @override
  Future<AppLocalization> load(Locale locale) => AppLocalization.load(locale);

  @override
  bool shouldReload(LocalizationsDelegate<AppLocalization> old) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant