GithubHelp home page GithubHelp logo

demezy / localize_and_translate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from msayed-net/localize_and_translate

0.0 0.0 0.0 1.67 MB

Flutter localization in easy steps

License: MIT License

Shell 1.15% Ruby 13.11% C++ 9.74% C 2.88% Objective-C 0.08% Kotlin 0.29% Dart 47.34% Swift 3.44% HTML 2.50% CMake 19.48%

localize_and_translate's Introduction

localize_and_translate

Flutter localization in easy steps

License Pub Example

PUB GitHub release GitHub stars GitHub forks

Getting Started

๐Ÿ”ฉ Installation

Add to your pubspec.yaml:

dependencies:
  localize_and_translate: <last_version>

Create folder and add translation files like this

assets
โ””โ”€โ”€ lang
    โ”œโ”€โ”€ {languageCode}.{ext}                  //only language code
    โ””โ”€โ”€ {languageCode}-{countryCode}.{ext}    //or full locale code

Example:

assets
โ””โ”€โ”€ lang
    โ”œโ”€โ”€ en.json
    โ””โ”€โ”€ en-US.json 

Declare your assets localization directory in pubspec.yaml:

flutter:
  assets:
    - assets/lang/

โš ๏ธ Note on iOS

For translation to work on iOS you need to add supported locales to ios/Runner/Info.plist as described here.

Example:

<key>CFBundleLocalizations</key>
<array>
  <string>en</string>
  <string>nb</string>
</array>

โš™๏ธ Configuration

Add EasyLocalization widget like in example

import 'package:flutter/material.dart';
import 'package:localize_and_translate/localize_and_translate.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
    await LocalizeAndTranslate.init(
        assetLoader: const AssetLoaderRootBundleJson('assets/lang/'), // <-- change the path of the translation files
        supportedLanguageCodes: <String>['ar', 'en'], // <-- or supportedLocales: [Locale('ar', 'DZ'), Locale('en', 'US')],
    );
  
    runApp(
        LocalizedApp(
            child: MaterialApp(
              // style 1
              builder: LocalizeAndTranslate.directionBuilder,
              // style 2
              builder: (BuildContext context, Widget? child) {
                child = LocalizeAndTranslate.directionBuilder(context, child);
        
                return child;
              },
              home: const MyHomePage(),
              locale: context.locale,
              localizationsDelegates: context.delegates,
              supportedLocales: context.supportedLocales,
            ),
        ),
    );
}

Full example

๐Ÿ“œ Localize And Translate init properties

Properties Required Description
supportedLanguageCodes or next List of supported languages to be converted to locales.
supportedLocales or prev List of supported locales.
defaultType false Path to your folder with localization files.
assetLoader true Class loader for localization values. You can create your own class.

Usage

Init

Call LocalizeAndTranslate.init(params) in your main before runApp.

void main() async{
  // ...
  // Needs to be called so that we can await for LocalizeAndTranslate.init();
  WidgetsFlutterBinding.ensureInitialized();

  await LocalizeAndTranslate.init(
    assetLoader: const AssetLoaderRootBundleJson('assets/lang/'), // <-- change the path of the translation files
    supportedLocales: <Locale>[Locale('ar', 'DZ'), Locale('en', 'US')], // <-- or  supportedLanguageCodes: <String>['ar', 'en'],
    defaultType: LocalizationDefaultType.asDefined, // <-- change the default type
  );
  // ...
  runApp(
    // ...
  );
  // ...
}

context extensions

LocalizeAndTranslate uses extension methods [BuildContext] for access to some values.

Example:

// set locale
context.setLocale(Locale('en', 'US'));

// set language code
context.setLanguageCode('en');

// get locale
context.locale; // en_US

// get language code
context.languageCode; // en

Translate tr()

Main function for translate your language keys

print('title'.tr(defaultValue: 'Awesome App')); //String

Translations

as json values pair

{
  "title": "Awesome App",
  "hello": "Hello",
  "world": "World!",
}

API Reference

Properties Extension Type Description
countryCode context Property Gets the country code of the current locale.
delegates context Property Gets the list of localization delegates used for translation.
init no Method Initializes the plugin with the desired configuration values.
locale context Property Gets the current locale being used for localization.
resetLocale context Method Resets the current locale to its default value.
setLanguageCode context Method Sets the language code for localization.
setLocale context Method Sets the current locale to one of the supported locales.
supportedLocales context Property Gets the list of supported locales for the app.
tr string Method Retrieves the translated string for a given localization key.

Reset everything to default values passed through init().

Example:

RaisedButton(
  onPressed: (){
    context.resetLocale();
  },
  child: Text(LocaleKeys.reset_locale).tr(),
)

Contributors

Contributors

localize_and_translate's People

Contributors

msayed-net avatar allcontributors[bot] avatar mo-ah-dawood avatar ryoksec avatar imgbotapp 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.