-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mineral-dart/develop
feat: Release 2.0.0
- Loading branch information
Showing
12 changed files
with
260 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Publish to pub.dev | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v[0-9]+.[0-9]+.[0-9]+*' # tag pattern on pub.dev: 'v' | ||
# Publish using custom workflow | ||
jobs: | ||
publish: | ||
permissions: | ||
id-token: write | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: dart-lang/setup-dart@v1 | ||
- name: Install dependencies | ||
run: dart pub get | ||
- name: Publish | ||
run: dart pub publish --force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
## 1.0.0 | ||
## 2.0.0 | ||
- Rewrite package to follow new `mineral: ^3.1.0` version of Core. | ||
|
||
## 1.1.0 | ||
- Write readme. | ||
|
||
## 1.0.0 | ||
- Initial version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,74 @@ | ||
# 🌐 I18n | ||
|
||
The I18n module allows you to translate your textual content into multiple languages from yaml files while injecting your variables. | ||
The i18n module has been designed exclusively for the Mineral framework, it allows you to translate your textual content through yaml files. | ||
|
||
## Register the module | ||
|
||
After installing the module, please register it within `./src/main.dart` following the scheme below : | ||
```dart | ||
Future<void> main () async { | ||
final i18n = I18n([Lang.fr, Lang.enGB]); | ||
Kernel kernel = Kernel() | ||
..intents.defined(all: true) | ||
..plugins.use([i18n]); | ||
await kernel.init(); | ||
} | ||
``` | ||
|
||
## Translate your textual content | ||
As a first step, please create a `lang` folder containing `{lang}.yaml` translation files. | ||
|
||
We consider the following software structure : | ||
``` | ||
lang/ | ||
foo/ | ||
fr.yaml | ||
en.yaml | ||
``` | ||
The files will contain the following keys : | ||
```yaml | ||
# lang/foo/fr.yaml | ||
bar: bar en français ! | ||
``` | ||
```yaml | ||
# lang/foo/en.yaml | ||
bar: bar in english ! | ||
``` | ||
Then we can use the `t()` function to translate our key path. | ||
|
||
```dart | ||
import 'package:mineral_i18n/mineral_i18n.dart'; | ||
class Foo extends MineralEvent<Ready> with Translation { | ||
Future<void> handle (Ready event) async { | ||
final String sentence = t(Lang.fr, 'foo.bar'); | ||
print(sentence); // bar en français ! | ||
final String sentence = t(Lang.en_GB, 'foo.bar'); | ||
print(sentence); // bar in english ! | ||
} | ||
} | ||
``` | ||
|
||
## Injecting variables | ||
The i18n module integrates the possibility of using variables thanks to special characters which will be replaced by the associated variable. | ||
|
||
We consider the file `lang/foo/en.yaml` as containing the following key set : | ||
```yaml | ||
bar: {framework} is my favourite framework ! | ||
``` | ||
|
||
Our string is now waiting for a variable named xx which we will give it when we call the `t()` function. | ||
```dart | ||
import 'package:mineral_i18n/mineral_i18n.dart'; | ||
class Foo extends MineralEvent<Ready> with Translation { | ||
Future<void> handle (Ready event) async { | ||
final String sentence = t(Lang.en_GB, 'foo.bar', { 'framework': 'Mineral' }); | ||
print(sentence); // Mineral is my favourite framework ! | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
library i18n; | ||
|
||
export 'src/i18n.dart'; | ||
export 'src/lang.dart'; | ||
export 'src/contracts/i18n_contract.dart'; | ||
export 'src/mixins/translation.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'dart:io'; | ||
|
||
abstract class I18nContract { | ||
List<String> get languages; | ||
Directory get langPath; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
8 changes: 3 additions & 5 deletions
8
lib/src/translation.dart → lib/src/managers/translation_manager.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:mineral_i18n/mineral_i18n.dart'; | ||
import 'package:mineral_ioc/ioc.dart'; | ||
|
||
mixin Translation { | ||
/// Translates the sentence defined by the key set into the requested language. | ||
/// Replacement parameters can be injected. | ||
/// ```dart | ||
/// final String sentence = t('en', 'foo.bar'); | ||
/// print(sentence); 👈 'Hello {user}' | ||
/// | ||
/// final String sentence = t('en', 'foo.bar', replacers { 'user': 'Freeze' }); | ||
/// print(sentence); 👈 'Hello Freeze' | ||
/// ``` | ||
String t (String lang, String key, { Map<String, dynamic>? replacers }) { | ||
dynamic target = ioc.use<I18n>().translationManager.cache[lang]; | ||
for (final element in key.split('.')) { | ||
target = target[element]; | ||
} | ||
|
||
if (replacers != null) { | ||
for (final replacer in replacers.entries) { | ||
target = target.toString().replaceAll('{${replacer.key}}', replacer.value); | ||
} | ||
} | ||
|
||
return target; | ||
} | ||
} |
Oops, something went wrong.