Polyglothq Dart CLI
THIS IS AN INTERNAL TOOL AND MIGHT NOT BE USEFUL FOR YOU
polyglothq
is a Dart CLI to support using Polyglot for Flutter
projects. polyglothq
fetches translations from polyglothq.com, and converts them to
a Flutter-suitable form.
How to install
You can install polyglothq
by running the following command in your terminal.
dart pub global activate polyglothq
Usage
Authorize polyglot
Before using polyglothq
, you'll need to authorize with your Polyglot token. Run the following command to authorize:
polyglothq authorize <token>
This command will store your token securely, allowing you to use polyglothq
with the necessary permissions.
Initialize polyglot in your project
To start using polyglothq
in your project, run the following command in the directory that contains your project:
polyglothq init
You'll be asked to select your project from the list of polyglot projects.
Pull translations from polyglot
Run the following command to pull translations from Polyglot.
polyglothq pull
For dynamic texts, you can use following command.
polyglothq pull --dynamic
What to do if I have the same language with different country codes?
Use the defaultCountryCode
section in polyglot.yml
to specify default country code.
:defaultCountryCode: en: GB sw: SW
Flutter usage
First create new file in the root of the project 'l10n.yaml' with the following content:
arb-dir: "./assets/i18n/"
template-arb-file: intl_en.arb
output-localization-file: app_localizations.dart
You will need to add 'strings.dart' file to the util/localization folder:
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class Strings {
Strings._();
static AppLocalizations? _latestLocalizations;
static AppLocalizations of(BuildContext context) => AppLocalizations.of(context) ?? latest;
static AppLocalizations get latest {
final appLocalizations = _latestLocalizations;
if (appLocalizations == null) {
throw 'Unable to load App Localizations';
}
return appLocalizations;
}
}
class StringsLocaleWidget extends StatelessWidget {
const StringsLocaleWidget({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
Strings._latestLocalizations = AppLocalizations.of(context);
return child;
}
}
class AppLocales {
static Locale get localeEn => const Locale('en', '');
static List<Locale> get values => <Locale>[
AppLocales.localeEn,
];
}
In your flutter code,
Strings.of(context).keyName
// With parameters
Strings.of(context).keyName('arg0', 'arg1', ...)
In cases, where you don't have access to the context:
- You'll need first to add the following widget:
MaterialApp(
builder: (context, child) {
return StringsLocaleWidget(child: child!); // Use StringsLocaleWidget
},
);
- Then you can use:
Strings.latest.keyName
- NOTE: Using translations without context is highly discouraged, and should only be used in special cases.
Libraries
- polyglothq
- A Flutter CLI for managing polyglot's translations.