Dart 애플리케이션에서 템플릿 문자열을 파싱하고 렌더링하기 위해 설계된 가벼운 명령줄 도구입니다. 자리 표시자를 실제 값으로 치환함으로써 동적 텍스트 생성을 가능하게 하며, 로컬라이제이션, 구성 또는 구조화된 출력 생성에 적합합니다. 패키지는 간단히 통합할 수 있으며 기본 템플릿 구문을 지원하여 개발자가 효율적으로 재사용 가능한 텍스트 패턴을 만들 수 있습니다. Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼에서 실행됩니다.
사용 사례
동적 메시지 생성
로컬라이제이션 템플릿
구성 파일 렌더링
코드 생성 유틸리티
사용자 인터페이스 문자열 템플릿
주요 기능
간단한 자리 표시자 치환
크로스 플랫폼 호환성
명령줄 인터페이스
경량이고 빠름
중첩 템플릿을 지원합니다
적합한 대상
기본 템플릿 렌더링이 필요한 개발자
현지화된 텍스트 출력이 필요한 프로젝트
동적 콘텐츠를 가진 CLI 도구
교육 목적
소규모 텍스트 처리
고려 사항
기본 템플릿 구문에 한정
고급 형식 지정 옵션이 없습니다
복잡한 템플릿 요구 사항에 적합하지 않습니다
형식이 잘못된 템플릿에 대한 내장 오류 처리 없음
간단한 사용 사례에만 최적
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Flutter Google Sheet localizations generator
Generates a localizations delegate from an online Google Sheet file.
Extract from the link the DOCID and SHEETID values : https://docs.google.com/spreadsheets/d/<DOCID>/edit#gid=<SHEETID>) :
2. Declare a localization delegate
Declare the following AppLocalizationsDelegate class with the SheetLocalization annotation pointing to your sheet in a lib/localization.dart file :
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.classAppLocalizationsDelegateextendsLocalizationsDelegate<AppLocalizationsData> {
constAppLocalizationsDelegate();
@overrideboolisSupported(Locale locale) => localizedLabels.containsKey(locale);
@overrideFuture<AppLocalizationsData> load(Locale locale) =>
SynchronousFuture<AppLocalizationsData>(localizedLabels[locale]!);
@overrideboolshouldReload(AppLocalizationsDelegate old) => false;
}
3. Generate your localizations
Run the following command to generate a lib/localization.g.dart file :
flutter packages pub run build_runner build
4. Configure your app
Update your Flutter app with your newly created delegate :
final labels = AppLocalizations.of(context);
print(labels.dates.month.february);
print(labels.templated.hello(firstName: "World"));
print(labels.templated.contact(Gender.male, lastName: "John"));
Regeneration
Because 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 version parameter of the @SheetLocalization annotation solves this issue.
Each time you want to trigger a new generation, simply increment that version number and call the build runner again.
then each supported language code ("en", "fr", ...)
Following rows for labels
Column 0 : the label key (can be a hierarchy, separated by dots)
then each translation based on language code of the column
Ignoring a column
Sometimes 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 :
Key
(Comments)
fr
en
example.man(Gender.male)
This is a man title on home page
homme
man
example.man(Gender.female)
This is a woman title on home page
femme
woman
Conditionals
It 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 (<ConditionName>.<ConditionCase).
You can also add one of the compatible types (int, double, num, DateTime) to the parameter by suffixing its key with :<type>.
/// Sheet
values.price, "The price is {{price:double}}\$"/// Codeprint(labels.values.price(price: 10.5));
Formatted parameters
You can indicate how the templated value must be formatted by ending the value with a formatting rule in brackets [<rule-key>]. This can be particulary useful for typed parameters.
The available formatting rules depend on the type and generally rely on the intl package.
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.