Skip to content

Commit ec14526

Browse files
committed
Add pluralization support
1 parent 7e4a4bd commit ec14526

22 files changed

+849
-97
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
* edited and clarified the documentation
66

77
## 0.0.2
8-
* remove the reset function
8+
* remove the reset function
9+
10+
## 0.0.3
11+
* add support for (basic) pluralization

README.md

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
**Until this package reaches 1.0.0, every version update *could* contain breaking changes. I'll try to deprecate things a few versions ahead**
22

33
# Signal translator
4-
For my own project, I needed a lightweight translation solution that could be used in Flutter.
4+
This is a translation package for the Signals framework. It's not a pluralization package.
5+
6+
For my own project, I needed a lightweight translation solution that could be used in dart.
57
I previously used the `easy_localization` package, but I didn't like how it made my app re-render.
68
This is not because of the package itself, but because I'm a Signals enthusiast.
7-
This package currently fits all my needs, but it might not fit yours.
9+
This package currently fits all my needs, but it might not fit yours.
10+
Please read the (non) features section to see if this package fits your needs.
11+
And feel free to open an issue or a PR if you want to add something.
812

913
## License
10-
Licensed under a modified MIT License.
14+
Licensed under a MIT License.
1115

1216
## (Non) features
13-
* Supports singular only, does not support pluralization
14-
* Only supports JSON translation files
15-
* Does not support dates
16-
17-
* Translates using signals
17+
* Supports singular and pluralization
1818
* Display app in system language by default
1919
* Support setting a different language
2020
* Supports setting system language as default
21+
* **Only supports JSON translation files**
22+
* **Does not support dates**
23+
2124

2225
## Getting started
2326
### important
@@ -40,7 +43,7 @@ Licensed under a modified MIT License.
4043
3. Add the following to your `pubspec.yaml` file:
4144
```yamld
4245
dependencies:
43-
signals_translator: ^0.0.1+2
46+
signals_translator: ^0.0.3
4447
4548
[...]
4649
@@ -74,16 +77,70 @@ SignalTranslator().currentLocale;
7477
```
7578
This is done automatically when the app starts, but if you want to build in a language selector, you can use this method to highlight the currently selected language.
7679

77-
### Full example
78-
See the 'example' folder for a complete example.
80+
## Examples
81+
### Basic translation (tl)
82+
```json
83+
{
84+
"English": "English"
85+
}
86+
```
87+
```dart
88+
Text(tl('English'));
89+
```
90+
91+
### Translation with a variable (tlv)
92+
```json
93+
"{0} has won the game!": "{0} has won the game!"
94+
```
95+
```dart
96+
Text(tlv('{0} has won the game!', 'David'));
97+
```
98+
99+
### Translation with multiple variables (tlvm)
100+
```json
101+
"He came in {0}, while his partner came in at the {1} place": "He came in {0}, while his partner came in at the {1} place",
102+
```
103+
```dart
104+
Text(tlvm('He came in {0}, while his partner came in at the {1} place', ['first', 'second']));
105+
```
106+
107+
### Pluralization (tlp)
108+
```json
109+
"I have {0} apples": {
110+
"zero": "I have no apples",
111+
"one": "I have 1 apple",
112+
"other": "I have {0} apples"
113+
},
114+
```
115+
```dart
116+
Text(tlp('I have {0} apples', 0)); // I have no apples
117+
Text(tlp('I have {0} apples', 1)); // I have 1 apple
118+
Text(tlp('I have {0} apples', 5)); // I have 5 apples
119+
```
79120

121+
### Pluralization with multiple counts (tlpm)
122+
```json
123+
"I have {0} strawberries and {1} bananas": {
124+
"zero_zero": "I have no strawberries and no bananas",
125+
"one_one": "I have 1 strawberry and 1 banana",
126+
"one_other": "I have 1 strawberry and {1} bananas",
127+
"other_one": "I have {0} strawberries and 1 banana",
128+
"other_other": "I have bo strawberries and {1} bananas"
129+
}
130+
```
80131

81-
## Additional features
82-
More features could be implemented. As my time is limited, feel free to open an issue and I will look into when I have time.
83-
- Planned for Q3: a dropdown menu to select the language
132+
```dart
133+
Text(tlpm('I have {0} strawberries and {1} bananas', [1, 1])); // I have 1 strawberry and 1 banana
134+
Text(tlpm('I have {0} strawberries and {1} bananas', [0, 0])); // I have no strawberries and no bananas
135+
Text(tlpm('I have {0} strawberries and {1} bananas', [2, 3])); // I have bo strawberries and 3 bananas
136+
```
84137

85-
## Credits
86-
All this is possible because of the best state management package out there: [Signals](https://pub.dev/packages/signals). Credits should go there :)
138+
#### Note on pluralization:
139+
For English, the text 'I have no strawberries and no bananas' is grammarly same as 'I have 0 strawberries and 0 bananas'. That means that you don't have to add a 'zero' entry for 'no strawberries and no bananas'. zero and other is sufficient. But for
140+
See the 'example' folder for a complete example.
87141

88142
## Development
89-
Publish extension using dart pub publish --dry-run
143+
Publish extension using dart pub publish --dry-run
144+
145+
## Considerations
146+
* Plural 'like there are / is 5 / winners' is not supported yet, might consider ICU here for the future..

example/.metadata

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,20 @@
44
# This file should be version controlled and should not be manually edited.
55

66
version:
7-
revision: "c23637390482d4cf9598c3ce3f2be31aa7332daf"
8-
channel: "stable"
7+
revision: "archlinuxaur0000000000000000000000000000"
8+
channel: ""
99

1010
project_type: app
1111

1212
# Tracks metadata for the flutter migrate command
1313
migration:
1414
platforms:
1515
- platform: root
16-
create_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
17-
base_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
18-
- platform: android
19-
create_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
20-
base_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
21-
- platform: ios
22-
create_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
23-
base_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
24-
- platform: web
25-
create_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
26-
base_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
27-
- platform: windows
28-
create_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
29-
base_revision: c23637390482d4cf9598c3ce3f2be31aa7332daf
16+
create_revision: archlinuxaur0000000000000000000000000000
17+
base_revision: archlinuxaur0000000000000000000000000000
18+
- platform: linux
19+
create_revision: archlinuxaur0000000000000000000000000000
20+
base_revision: archlinuxaur0000000000000000000000000000
3021

3122
# User provided section
3223

example/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# signals_translator_example
2+
3+
A new Flutter project.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Flutter application.
8+
9+
A few resources to get you started if this is your first Flutter project:
10+
11+
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
12+
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
13+
14+
For help getting started with Flutter development, view the
15+
[online documentation](https://docs.flutter.dev/), which offers tutorials,
16+
samples, guidance on mobile development, and a full API reference.

example/assets/translations/en.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
"Dutch": "Dutch",
55
"English": "English",
66
"Spanish": "Spanish",
7+
"{0} has won the game!": "{0} has won the game!",
78
"He came in {0}, while his partner came in at the {1} place": "He came in {0}, while his partner came in at the {1} place",
89
"CAN_USE_KEY": "You can also use key-value pairs to translate",
9-
"Signal Translator Example": "Signal Translator Example"
10+
"Signal Translator Example": "Signal Translator Example",
11+
"I have {0} apples": {
12+
"zero": "I have no apples",
13+
"one": "I have 1 apple",
14+
"other": "I have {0} apples"
15+
},
16+
"I have {0} strawberries and {1} bananas": {
17+
"zero_zero": "No tengo fresas ni plátanos",
18+
"one_one": "Tengo 1 fresa y 1 plátano",
19+
"one_other": "Tengo 1 fresa y {1} plátanos",
20+
"other_one": "Tengo {0} fresas y 1 plátano",
21+
"other_other": "Tengo {0} fresas y {1} plátanos"
22+
}
1023
}
1124
}

example/assets/translations/es.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
"Dutch": "Holandés",
55
"English": "Inglés",
66
"Spanish": "Español",
7+
"{0} has won the game!": "{0} ha ganado la partida!",
78
"He came in {0}, while his partner came in at the {1} place": "Él llegó en {0}, mientras que su pareja llegó en el {1} lugar",
89
"CAN_USE_KEY": "También puedes usar pares clave-valor para traducir",
9-
"Signal Translator Example": "Ejemplo de traductor de 'Signals'"
10+
"Signal Translator Example": "Ejemplo de traductor de 'Signals'",
11+
"I have {0} apples": {
12+
"zero": "No tengo ninguna manzana",
13+
"one": "Tengo una manzana",
14+
"other": "Tengo {0} manzanas"
15+
},
16+
"I have {0} strawberries and {1} bananas": {
17+
"zero_zero": "No tengo fresas ni plátanos",
18+
"one_one": "Tengo 1 fresa y 1 plátano",
19+
"one_other": "Tengo 1 fresa y {1} plátanos",
20+
"other_one": "Tengo {0} fresas y 1 plátano",
21+
"other_other": "Tengo {0} fresas y {1} plátanos"
22+
}
1023
}
1124
}

example/assets/translations/nl.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
"Dutch": "Nederlands",
55
"English": "English",
66
"Spanish": "Español",
7+
"{0} has won the game!": "{0} heeft het spel gewonnen!",
78
"He came in {0}, while his partner came in at the {1} place": "Zijn parter eindigde op de {1} plaats, terwijl hij op de {0} plaats eindigde",
89
"CAN_USE_KEY": "Je kunt ook sleutel-waardeparen gebruiken om te vertalen",
9-
"Signal Translator Example": "'Signal' vertaler voorbeeld"
10+
"Signal Translator Example": "'Signal' vertaler voorbeeld",
11+
"I have {0} apples": {
12+
"zero": "I heb geen appels",
13+
"one": "I heb een appel",
14+
"other": "Ik heb {0} appels"
15+
},
16+
"I have {0} strawberries and {1} bananas": {
17+
"zero_zero": "Ik heb geen aardbeien en geen bananen",
18+
"one_one": "Ik heb 1 aardbei en 1 banaan",
19+
"one_other": "Ik heb 1 aardbei en {1} bananen",
20+
"other_one": "Ik heb {0} aardbeien en 1 banaan",
21+
"other_other": "Ik heb {0} aardbeien en {1} bananen"
22+
}
1023
}
1124
}

example/lib/main.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,32 @@ void main() async {
3838
),
3939
],
4040
),
41+
Text('Translate (tl) examples:', style: TextStyle(fontWeight: FontWeight.bold)),
4142
Text(tl('Dutch')),
4243
Text(tl('English')),
4344
Text(tl('Spanish')),
45+
Text(tl('CAN_USE_KEY')),
46+
Text('Tranlate with variable (tlv) example:', style: TextStyle(fontWeight: FontWeight.bold)),
4447
Text(
4548
tlv(
49+
'{0} has won the game', 'David',
50+
),
51+
),
52+
Text('Translate with multiple variables (tlmv) example:', style: TextStyle(fontWeight: FontWeight.bold)),
53+
Text(
54+
tlvm(
4655
'He came in {0}, while his partner came in at the {1} place',
4756
['first', 'second'],
4857
),
4958
),
50-
Text(tl('CAN_USE_KEY')),
59+
Text('Pluralization (tlp) examples:', style: TextStyle(fontWeight: FontWeight.bold)),
60+
Text(tlp('I have {0} apples', 0) ?? ''), // I have no apples
61+
Text(tlp('I have {0} apples', 1) ?? ''), // I have 1 apple
62+
Text(tlp('I have {0} apples', 5) ?? ''), // I have 5 apples
63+
Text('Pluralization with multiple counts (tlpm) examples:', style: TextStyle(fontWeight: FontWeight.bold)),
64+
Text(tlpm('I have {0} strawberries and {1} bananas', [1, 1]) ?? ''), // I have 1 strawberry and 1 banana
65+
Text(tlpm('I have {0} strawberries and {1} bananas', [0, 0]) ?? ''), // I have no strawberries and no bananas
66+
Text(tlpm('I have {0} strawberries and {1} bananas', [2, 3]) ?? ''), // I have bo strawberries and 3 bananas
5167
],
5268
),
5369
),

example/linux/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flutter/ephemeral

0 commit comments

Comments
 (0)