Giter VIP home page Giter VIP logo

flutter-locale_switcher's Introduction

locale_switcher

A widget for switching the locale of your application.

codecov

Content

About

This package allows you to add locale-switching functionality to your app with just 2 lines of code.

This is NOT a localization package, it is just a few useful widgets that extend functionality of localization systems, such as: intl, easy_localization, etc...

Note: This package is already small, but if you still want to further reduce its size, you can use locale_switcher_dev package, which allow you to control what flags are included (or none) and which packages it depend on (or none).

Features

Usage

  1. Wrap MaterialApp or CupertinoApp with LocaleManager (example for intl package):
  @override
Widget build(BuildContext context, WidgetRef ref) {
  return LocaleManager(
      child: MaterialApp(
          locale: LocaleSwitcher.localeBestMatch,
          supportedLocales: AppLocalizations.supportedLocales,
          localizationsDelegates: AppLocalizations.localizationsDelegates,
  //...
  1. Add LocaleSwitcher widget anywhere into your app.

Troubleshooting

Note: localization should be set up before you start to use this package, if there some problems - please, check next section and/or documentation of localization system you use, before reporting bug.

For intl package

Check that intl package is set up correctly, here is an example setup of pubspec.yaml and l10n.yaml

For easy_localization package

Check that easy_localization package is set up correctly, here is an example setup of pubspec.yaml.

Note: if you use locale_switcher_dev you don't need to use LocaleSwitcher.setLocaleCallBack.

But for locale_switcher it is required setLocaleCallBack: (context) => context.setLocale(LocaleSwitcher.localeBestMatch)

For slang package

Check that slang package is set up correctly, here is an example setup of pubspec.yaml.

Currently it didn't work without: LocaleSwitcher.setLocaleCallBack, see example how to setup it - here

Examples

Online Example here

With intl package:

With easy_localization package:

With slang package:

TODO:

  • Improve rectangle flags!

FAQ

- How to change order of languages?

Languages are shown in the same order as they listed in l10n.yaml, or dynamically via LocaleSwitcher.custom.

- How to change flag of language?

Use LocaleManager.reassign parameter like this:

LocaleManager(
  reassign: {'en': ['GB', 'English', <Your_icon_optional>]}
  // (first two options are required, third is optional)
  // first option is the code of country which flag you want to use
...
)

- How to use localization outside of MaterialApp(or CupertinoApp)?

Here is a useful example, although it is not depend on this package:

import 'package:flutter_gen/gen_l10n/app_localizations.dart';

/// Access localization through locale
extension LocaleWithDelegate on Locale {
  /// Get class with translation strings for this locale.
  AppLocalizations get tr => lookupAppLocalizations(this);
}

Locale("en").tr.example
// or 
LocaleManager.locale.value.tr.example

flutter-locale_switcher's People

Contributors

alexqwesa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.