Coder Social home page Coder Social logo

riverpod-localization's Introduction

Riverpod Localization

A demonstration of using Riverpod for dynamic locale switching in-app, with persistence.

drawing

How It Works

  • Fallback Locale: Declared inside locale_state.dart, and set with the @Default decorator.
  • Default Locale: At startup, gets the system's Platform.localeName (via dart:io) and uses it to set the app's locale.
    • If the platform locale does not match a supported locale
      • The first matching language code locale will be set.
      • Otherwise, the fallback locale will be used.
  • Supported Locales: These are stored in a provider, accessible from anywhere. Update this list to add more locales.
  • Current Locale: Accessible from a provider anywhere the ref object is available. Watch this provider to rebuild whenever the locale changes. You can still use Localizations.localeOf(context) to get the Locale.

Locale Changes

  • initState sets the initial locale from the platform.

    • App starts up with fallback locale
    • initState immediately attempts to restore a locale from persistent storage to Riverpod state.
    • If there is no locale in storage, attempts to use the Platform's locale.
  • The drop down button can be used to select a new locale.

    • This updates state and saves the locale to persistent storage.
  • The MaterialApp() is wrapped in a Riverpod ConsumerWidget which will rebuild automatically anytime the watched ref.watch() locale value changes.

  • All translated strings are rebuilt as part of this process.

Platform Locale

The Platform.locale property of dart:io library only works on mobile. For web, we need to use dart:html's window.navigator.language. This is implemented with the PlatformLocale() interface and conditional imports.

Dependencies

Getting Started

Code Generator: This app uses build_runner to generate code for freezed.

# flutter pub run build_runner build --delete-conflicting-outputs

riverpod-localization's People

Contributors

mdrideout avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

riverpod-localization's Issues

Localizations in providers

Hi! Thank you for you interesting repo.

Did you thought about a solution to have localized text in a provider itself. I'm using provider as viemodels, and it would be super handy if i could use a text from localization directly in the providers. But I have no build context in there.

Looking forward if you have an idea.

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.