Git Product home page Git Product logo

flutter_localisation's Introduction

Flutter Localisation

flutter_localisation

alt text

A small flutter test project to try out language handling and translations.

Run the following commands to create all the required generated files. The output folder needs to already exist.

Create .arb files

flutter pub pub run intl_translation:extract_to_arb --output-dir=lib/l10n/app/source lib/l10n/app/localizations.dart

This will create the file "intl_messages.arb". Copy this and replace "messages" in the filename with de/en (the supported languages), then run the following command:

Create message files

flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/l10n/app/messages lib/l10n/app/localizations.dart lib/l10n/app/source/intl_en.arb lib/l10n/app/source/intl_de.arb

Test translations load

Go in to the emulator phone settings and change the input language to german. Switch back to the app and it will update with the correct language.

Structure

There subfolder "app" in the l10n folder is there to seperate translations. In a big app it would be difficult to have all translations in one file, so I use the subfolders like namespaces. This way different sections of an app can have their own translation files. You could copy the whole app folder and call it "errors" for example to keep all error messages separate in one file.

Each "namespace" can have it's own translations and a delegate that must be added in main.dart.

Adding translations

Add new translations to the localization.dart file (lib/l10n/app/localizations.dart). Then run the first command listed above. This will create the arb messages file. Copy these and change to to reflect language (see above) and edit as needed.

Then run the second command listed above, this will create the messages that are actually loaded and used by the app.

Using Placehodlers

Create a function in localization.dart instead of a getter (getter's cannot accept arguments) and add the placeholders to the args array like so:

  String detailUpdatedOn(String updateddDate) {
    return Intl.message(
      'Updated $updateddDate',
      name: 'detailUpdatedOn',
      args: [updateddDate],
    );
  }

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.