csv_localizations 0.1.3 csv_localizations: ^0.1.3 copied to clipboard
Localize your Flutter app using a single CSV file for all languages.
csv_localizations #
Localize your Flutter app using a single CSV file
Usage #
See example
Install #
Add package to your pubspec.yaml
dependencies:
csv_localizations:
Add CSV asset #
Add a CSV translation file as an asset and describe it in your pubspec.yaml
Tip: Create a Spreadsheet via Google docs, then export as CSV
flutter:
assets:
- assets/lang.csv
CSV example file
Top row show supported language codes; rows below are localizations.
Left column are keys for localized values.
key, en, nb
Hi, Hi, Hei
Cheese, Cheese, Ost
my_img,assets/en.png,assets/nb.png,assets/da.png
Note: keys can point to local assets like images etc.
In code #
Add CsvLocalizationsDelegate
to MaterialApp
and set supportedLocales
using
language codes.
localizationsDelegates:[
... // global delegates
CsvLocalizationsDelegate(
CsvLocalizations(
assetPath: 'assets/lang.csv',
supportedLanguageCodes: ['en', 'nb'],
),
),
]
supportedLocales: [Locale('en'), Locale('nb')],
API #
Now translate strings using
CsvLocalizations.of(context).tr('Hi')
Or use the simpler String extension method
'Hi'.tr(context)
Or load a localized image
Image.asset('my_img'.tr(context))
Note on iOS #
Add supported locales to
ios/Runner/Info.plist
as described here.
Example:
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>nb</string>
<string>da</string>
</array>