dalocale

Creator: coderz1093

Last updated:

0 purchases

dalocale Image
dalocale Images

Languages

Categories

Add to Cart

Description:

dalocale

Dalocale #
A Flutter package to easily support internationalization and localization in your project.
Installation #
Add the following dependency to your pubspec.yaml:
dependencies:
flutter_localizations:
sdk: flutter

dev_dependencies:
dalocale: ^2.5.0
copied to clipboard
Example #
Creating localization files #
Create a folder to store all the localizations in json format. The name of each file should match the locale that is representing. For example:
└─ assets
└─ i18n
├─ en.json
└─ es.json
copied to clipboard
Each localization file must contain a single json object with all the keys and values. For example:
{
"greetings": "Hello, world!",
"welcome.back": "Welcome back: %1$s"
}
copied to clipboard
In the previous example, the key welcome.back contains a value with a parameter.
Parameters #
Parameters inside of localized text must be declared as %index$format, where:

index: the position of the parameter in the text (starting from 1)
format: the format of the parameter (e.g. s, d, f, etc.)

Generating Dart code #
To generate the Dart file containing all the localizations, run the following command:
flutter pub pub run dalocale:dalocale.dart INPUT_FOLDER OUTPUT_FILE [DEFAULT_LOCALE] [LIB_FOLDER]
copied to clipboard
For example:
flutter pub pub run dalocale:dalocale.dart ./assets/i18n/ ./lib/foo/bar/localizations.dart en ./lib
copied to clipboard
The LIB_FOLDER parameter is used to check for unused keys in the project.
Using generated code #
In you main.dart file, add the auto-generated classes CustomLocalizationsDelegate and Localized to the app:
import 'package:flutter/material.dart';
import 'package:flutter_library/foo/bar/localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ...,
localizationsDelegates: const [
CustomLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: Localized.locales,
);
}
}
copied to clipboard
You can use the class Localized to have access to all the entries declared in the json files:
Text(Localized.get.greetings)
copied to clipboard
Text(Localized.get.welcomeBack('John'))
copied to clipboard
To get the current active locale in the app:
Locale locale = Localized.current;
copied to clipboard

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.