在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):aloisdeniel/flutter_sheet_localization开源软件地址(OpenSource Url):https://github.com/aloisdeniel/flutter_sheet_localization开源编程语言(OpenSource Language):Dart 99.8%开源软件介绍(OpenSource Introduction):Flutter Google Sheet localizations generatorGenerates a localizations delegate from an online Google Sheet file. InstallAdd the following to your dependencies:
flutter_sheet_localization: <latest>
flutter_localizations:
sdk: flutter
dev_dependencies:
flutter_sheet_localization_generator: <latest>
build_runner: <latest> Usage1. Create a Google SheetCreate a sheet with your translations (following the bellow format, an example sheet is available here) : Make sure that your sheet is shared : Extract from the link the 2. Declare a localization delegateDeclare the following import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_sheet_localization/flutter_sheet_localization.dart';
part 'localization.g.dart';
@SheetLocalization("DOCID", "SHEETID", 1) // <- See 1. to get DOCID and SHEETID
// the `1` is the generated version. You must increment it each time you want to regenerate
// a new version of the labels.
class AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizationsData> {
const AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) => localizedLabels.containsKey(locale);
@override
Future<AppLocalizationsData> load(Locale locale) =>
SynchronousFuture<AppLocalizationsData>(localizedLabels[locale]!);
@override
bool shouldReload(AppLocalizationsDelegate old) => false;
}
3. Generate your localizationsRun the following command to generate a
4. Configure your appUpdate your Flutter app with your newly created delegate : MaterialApp(
locale: AppLocalizations.languages.keys.first, // <- Current locale
localizationsDelegates: [
const AppLocalizationsDelegate(), // <- Your custom delegate
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales:
AppLocalizations.languages.keys.toList(), // <- Supported locales
// ...
); 5. Display your labelsfinal labels = AppLocalizations.of(context);
print(labels.dates.month.february);
print(labels.templated.hello(firstName: "World"));
print(labels.templated.contact(Gender.male, lastName: "John")); RegenerationBecause of the caching system of the build_runner, it can't detect if there is a change on the distant sheet and it can't know if a new generation is needed. The Each time you want to trigger a new generation, simply increment that version number and call the build runner again. Google Sheet formatYou can see an example sheet here. Global formatThe file should have :
Ignoring a columnSometimes you may need to add comments for translators. For this, simply add a column with a name between parenthesis and the column will be completely ignored by the generator. Example :
ConditionalsIt is pretty common to have variants of a label based on a condition (for example: Genders, Plurals, ...). Simply duplicate your entries and end them with Example :
See example for more details. PluralsThe conditionals can be used the same way for plurals : Example :
From your Dart code, you can then define an extension : extension PluralExtension on int {
Plural plural() {
if (this == 0) return Plural.zero;
if (this == 1) return Plural.one;
return Plural.multiple;
}
} See example for more details. Dynamic labelsYou can insert a A Dart function will be generated to be used from your code.
Typed parametersYou can also add one of the compatible types (
Formatted parametersYou can indicate how the templated value must be formatted by ending the value with a formatting rule in brackets The available formatting rules depend on the type and generally rely on the
Examples:
Why ?I find the Flutter internationalization tools not really easy to use, and I wanted a simple tool for sharing translations. Most solutions also use string based keys, and I wanted to generate pure dart code to improve permormance. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论