Skip to content

Commit ba98b5e

Browse files
authored
add localization (#12)
1 parent fdac67e commit ba98b5e

File tree

9 files changed

+131
-82
lines changed

9 files changed

+131
-82
lines changed

l10n.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
template-arb-file: intl_en.arb
2+
output-localization-file: instreal_localizations.dart
3+
output-class: InstrealLocalizations
4+
preferred-supported-locales:
5+
- en
6+
use-deferred-loading: true
7+
untranslated-messages-file: untranslated-messages.json

lib/l10n/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Localization
2+
3+
## Creating mew localization
4+
5+
1. Add the title and string to `lib/l10n/intl_en.arb` for example:
6+
7+
8+
```dart
9+
"hello": "Hello",
10+
```
11+
12+
2. Add translated map to `lib/l10n/intl_fr.arb` like this:
13+
14+
```dart
15+
"hello": "Bonjour",
16+
```
17+
18+
3. By running app, flutter generate all Localization see *Generating
19+
Localizations*, but if you want to generate before running see
20+
*Generating Localizations in the command line*
21+
22+
4. To use localization first import `flutter_gen` like this:
23+
24+
```dart
25+
import 'package:instreal/l10n/index.dart';
26+
```
27+
and then use the localization:
28+
```dart
29+
InstrealLocalizations.of(context)!.hello
30+
```
31+
32+
### Note: if you have any untranslated message, flutter generates them in `untranslated-messages.json` and will be determined when you create a pull request to merge.
33+
34+
## Creating new locale messages with variable
35+
36+
When adding new strings to be localized, update `intl_en_.arb`, which is
37+
used by this project as the template. When creating new entries with variable, they
38+
have to be in the following format:
39+
40+
```arb
41+
"helloName": "Hello {name}",
42+
"@helloName": {
43+
"placeholders": {
44+
"name": {}
45+
}
46+
},
47+
```
48+
49+
In this example, `dartGetterVariableName` should be the Dart method/property
50+
name that you will be using in your localizations delegate.
51+
52+
After adding the new message in `intl_en.arb`, it can be used in the app by
53+
regenerating the Localizations delegate.
54+
This allows use of the English message through your localizations delegate in
55+
the application code immediately without having to wait for the translations
56+
to be completed.
57+
58+
## Generating localizations
59+
60+
Running the application for the first time should generate a synthetic package
61+
containing the app's localizations through importing
62+
`package:flutter_gen/gen_l10n/`. The code is generated will be generated in
63+
`.dart_tool/flutter_gen/gen_l10n/`.
64+
65+
The generated localizations code is updated every time `flutter run`
66+
is called, and during hot reload and restart. This means that updates to
67+
existing arb files can be made and seen in real time during app development.
68+
69+
### Generating localizations in the command line
70+
71+
First temporary remove `l10n.yaml` file and then from the root directory, run:
72+
73+
```bash
74+
flutter gen-l10n \
75+
--template-arb-file=intl_en.arb \
76+
--output-localization-file=instreal_localizations.dart \
77+
--output-class=InstrealLocalizations
78+
```
79+
Don't forget to restore `l10n.yaml` file!

lib/l10n/index.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'package:flutter_gen/gen_l10n/instreal_localizations.dart';

lib/l10n/intl_en.arb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"@@locale": "en",
3+
"hello": "Hello"
4+
}

lib/l10n/intl_fr.arb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"@@locale": "fr",
3+
"hello": "Bonjour"
4+
}

lib/main.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:instreal/l10n/index.dart';
23

34
void main() {
45
runApp(const MyApp());
@@ -12,6 +13,8 @@ class MyApp extends StatelessWidget {
1213
Widget build(BuildContext context) {
1314
return MaterialApp(
1415
title: 'Flutter Demo',
16+
localizationsDelegates: InstrealLocalizations.localizationsDelegates,
17+
supportedLocales: InstrealLocalizations.supportedLocales,
1518
theme: ThemeData(
1619
// This is the theme of your application.
1720
//
@@ -108,6 +111,9 @@ class _MyHomePageState extends State<MyHomePage> {
108111
const Text(
109112
'You have pushed the button this many times:',
110113
),
114+
Text(
115+
InstrealLocalizations.of(context)!.hello,
116+
),
111117
Text(
112118
'$_counter',
113119
style: Theme.of(context).textTheme.headlineMedium,

pubspec.lock

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ packages:
3737
dependency: transitive
3838
description:
3939
name: collection
40-
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
40+
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
4141
url: "https://pub.dev"
4242
source: hosted
43-
version: "1.17.1"
43+
version: "1.17.2"
4444
cupertino_icons:
4545
dependency: "direct main"
4646
description:
@@ -70,11 +70,24 @@ packages:
7070
url: "https://pub.dev"
7171
source: hosted
7272
version: "2.0.1"
73+
flutter_localizations:
74+
dependency: "direct main"
75+
description: flutter
76+
source: sdk
77+
version: "0.0.0"
7378
flutter_test:
7479
dependency: "direct dev"
7580
description: flutter
7681
source: sdk
7782
version: "0.0.0"
83+
intl:
84+
dependency: "direct main"
85+
description:
86+
name: intl
87+
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
88+
url: "https://pub.dev"
89+
source: hosted
90+
version: "0.18.1"
7891
js:
7992
dependency: transitive
8093
description:
@@ -103,10 +116,10 @@ packages:
103116
dependency: transitive
104117
description:
105118
name: material_color_utilities
106-
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
119+
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
107120
url: "https://pub.dev"
108121
source: hosted
109-
version: "0.2.0"
122+
version: "0.5.0"
110123
meta:
111124
dependency: transitive
112125
description:
@@ -132,10 +145,10 @@ packages:
132145
dependency: transitive
133146
description:
134147
name: source_span
135-
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
148+
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
136149
url: "https://pub.dev"
137150
source: hosted
138-
version: "1.9.1"
151+
version: "1.10.0"
139152
stack_trace:
140153
dependency: transitive
141154
description:
@@ -172,10 +185,10 @@ packages:
172185
dependency: transitive
173186
description:
174187
name: test_api
175-
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
188+
sha256: daadc9baabec998b062c9091525aa95786508b1c48e9c30f1f891b8bf6ff2e64
176189
url: "https://pub.dev"
177190
source: hosted
178-
version: "0.5.1"
191+
version: "0.5.2"
179192
vector_math:
180193
dependency: transitive
181194
description:

pubspec.yaml

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,24 @@
11
name: instreal
2-
description: A new Flutter project.
3-
# The following line prevents the package from being accidentally published to
4-
# pub.dev using `flutter pub publish`. This is preferred for private packages.
5-
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
6-
7-
# The following defines the version and build number for your application.
8-
# A version number is three numbers separated by dots, like 1.2.43
9-
# followed by an optional build number separated by a +.
10-
# Both the version and the builder number may be overridden in flutter
11-
# build by specifying --build-name and --build-number, respectively.
12-
# In Android, build-name is used as versionName while build-number used as versionCode.
13-
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
14-
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
15-
# Read more about iOS versioning at
16-
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
17-
# In Windows, build-name is used as the major, minor, and patch parts
18-
# of the product and file versions while build-number is used as the build suffix.
2+
description: Instreal Flutter project.
3+
publish_to: 'none'
194
version: 1.0.0+1
205

216
environment:
227
sdk: ^3.0.0
238

24-
# Dependencies specify other packages that your package needs in order to work.
25-
# To automatically upgrade your package dependencies to the latest versions
26-
# consider running `flutter pub upgrade --major-versions`. Alternatively,
27-
# dependencies can be manually updated by changing the version numbers below to
28-
# the latest version available on pub.dev. To see which dependencies have newer
29-
# versions available, run `flutter pub outdated`.
309
dependencies:
3110
flutter:
3211
sdk: flutter
33-
34-
35-
# The following adds the Cupertino Icons font to your application.
36-
# Use with the CupertinoIcons class for iOS style icons.
37-
cupertino_icons: ^1.0.2
12+
flutter_localizations:
13+
sdk: flutter
14+
intl: any
15+
cupertino_icons: ^1.0.5
3816

3917
dev_dependencies:
4018
flutter_test:
4119
sdk: flutter
20+
flutter_lints: ^2.0.1
4221

43-
# The "flutter_lints" package below contains a set of recommended lints to
44-
# encourage good coding practices. The lint set provided by the package is
45-
# activated in the `analysis_options.yaml` file located at the root of your
46-
# package. See that file for information about deactivating specific lint
47-
# rules and activating additional ones.
48-
flutter_lints: ^2.0.0
49-
50-
# For information on the generic Dart part of this file, see the
51-
# following page: https://dart.dev/tools/pub/pubspec
52-
53-
# The following section is specific to Flutter packages.
5422
flutter:
55-
56-
# The following line ensures that the Material Icons font is
57-
# included with your application, so that you can use the icons in
58-
# the material Icons class.
5923
uses-material-design: true
60-
61-
# To add assets to your application, add an assets section, like this:
62-
# assets:
63-
# - images/a_dot_burr.jpeg
64-
# - images/a_dot_ham.jpeg
65-
66-
# An image asset can refer to one or more resolution-specific "variants", see
67-
# https://flutter.dev/assets-and-images/#resolution-aware
68-
69-
# For details regarding adding assets from package dependencies, see
70-
# https://flutter.dev/assets-and-images/#from-packages
71-
72-
# To add custom fonts to your application, add a fonts section here,
73-
# in this "flutter" section. Each entry in this list should have a
74-
# "family" key with the font family name, and a "fonts" key with a
75-
# list giving the asset and other descriptors for the font. For
76-
# example:
77-
# fonts:
78-
# - family: Schyler
79-
# fonts:
80-
# - asset: fonts/Schyler-Regular.ttf
81-
# - asset: fonts/Schyler-Italic.ttf
82-
# style: italic
83-
# - family: Trajan Pro
84-
# fonts:
85-
# - asset: fonts/TrajanPro.ttf
86-
# - asset: fonts/TrajanPro_Bold.ttf
87-
# weight: 700
88-
#
89-
# For details regarding fonts from package dependencies,
90-
# see https://flutter.dev/custom-fonts/#from-packages
24+
generate: true

untranslated-messages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)