Skip to content

Commit

Permalink
feat: Migrate to Mineral package release
Browse files Browse the repository at this point in the history
  • Loading branch information
LeadcodeDev committed Mar 29, 2023
1 parent 2b99d38 commit df35f12
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 100 deletions.
1 change: 0 additions & 1 deletion lib/mineral_i18n.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
library i18n;

export 'src/i18n.dart';
export 'src/lang.dart';
export 'src/contracts/i18n_contract.dart';
export 'src/mixins/translation.dart';

4 changes: 1 addition & 3 deletions lib/src/contracts/i18n_contract.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:io';

import 'package:mineral_i18n/src/lang.dart';

abstract class I18nContract {
List<Lang> get languages;
List<String> get languages;
Directory get langPath;
}
37 changes: 14 additions & 23 deletions lib/src/i18n.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import 'dart:io';

import 'package:mineral_contract/mineral_contract.dart';
import 'package:mineral_i18n/src/contracts/i18n_contract.dart';
import 'package:mineral_i18n/src/lang.dart';
import 'package:mineral_i18n/src/translation_manager.dart';
import 'package:mineral_package/mineral_package.dart';
import 'package:mineral_i18n/src/managers/translation_manager.dart';
import 'package:path/path.dart';
import 'package:yaml/yaml.dart';

class I18n extends MineralPackage implements I18nContract {
@override
String namespace = 'Mineral/Plugins/I18n';

@override
String label = 'I18n';

@override
String description = '';

class I18n extends MineralPackageContract implements I18nContract {
TranslationManager translationManager = TranslationManager();
final List<Lang> _languages;

final List<String> _languages;
final String folder;

I18n(this._languages, { this.folder = 'lang' });
I18n(this._languages, { this.folder = 'lang' }): super('I18n', 'Official package');

/// ## Languages allowed
/// ```dart
/// final List<Lang> allowedLanguages = i18n.languages;
/// ```
@override
List<Lang> get languages => _languages;
List<String> get languages => _languages;


/// ## Languages root directory
Expand All @@ -38,13 +29,6 @@ class I18n extends MineralPackage implements I18nContract {
@override
Directory get langPath => Directory(join(root.path, folder));

/// Insert languages into i18n instance
void registerLanguages() {
for (final Lang lang in _languages) {
translationManager.cache.putIfAbsent(lang.normalize, () => {});
}
}

/// Initialize i18n package
@override
Future<void> init () async {
Expand All @@ -56,6 +40,13 @@ class I18n extends MineralPackage implements I18nContract {
_walk(langPath);
}

/// Insert languages into i18n instance
void registerLanguages() {
for (final lang in _languages) {
translationManager.cache.putIfAbsent(lang, () => {});
}
}

/// Recursively browses folders to extract translations
_walk (Directory directory) {
final location = directory.path.split(separator).last;
Expand Down
43 changes: 0 additions & 43 deletions lib/src/lang.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'package:mineral_i18n/src/lang.dart';

class TranslationManager {
final Map<String, dynamic> _cache = {};

Map<String, dynamic> get cache => _cache;

void addTranslations (Lang lang, Map<String, String> translations) {
void addTranslations (final String lang, Map<String, String> translations) {
for (final translation in translations.entries) {
dynamic location = _cache[lang.normalize];
dynamic location = _cache[lang] ?? {};
List<String> keys = translation.key.split('.');

for (final key in keys) {
Expand Down
9 changes: 2 additions & 7 deletions lib/src/mixins/translation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import 'package:mineral_i18n/mineral_i18n.dart';
import 'package:mineral_ioc/ioc.dart';

mixin Translation {
I18n _getPlugin () {
final dynamic pluginManager = ioc.services.entries.firstWhere((element) => element.key.toString() == 'PluginManagerCraft').value;
return pluginManager.use<I18n>();
}

/// Translates the sentence defined by the key set into the requested language.
/// Replacement parameters can be injected.
/// ```dart
Expand All @@ -16,8 +11,8 @@ mixin Translation {
/// final String sentence = t(Lang.enGB, 'foo.bar', { 'user': 'Freeze' });
/// print(sentence) 👈 'Hello Freeze'
/// ```
String t (Lang lang, String key, { Map<String, dynamic>? replacers }) {
dynamic target = _getPlugin().translationManager.cache[lang.normalize];
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];
}
Expand Down
61 changes: 55 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.5"
http_multi_server:
dependency: transitive
description:
Expand Down Expand Up @@ -148,20 +155,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
mineral_ioc:
mineral:
dependency: "direct main"
description:
name: mineral_ioc
name: mineral
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
mineral_package:
version: "3.1.0"
mineral_cli:
dependency: transitive
description:
name: mineral_cli
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
mineral_console:
dependency: transitive
description:
name: mineral_console
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
mineral_contract:
dependency: "direct main"
description:
name: mineral_package
name: mineral_contract
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
mineral_environment:
dependency: transitive
description:
name: mineral_environment
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
mineral_ioc:
dependency: "direct main"
description:
name: mineral_ioc
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
node_preamble:
dependency: transitive
description:
Expand Down Expand Up @@ -197,6 +232,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
recase:
dependency: transitive
description:
name: recase
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
shelf:
dependency: transitive
description:
Expand Down Expand Up @@ -295,6 +337,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.16"
tint:
dependency: transitive
description:
name: tint
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -338,4 +387,4 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=2.17.1 <3.0.0"
dart: ">=2.18.1 <3.0.0"
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ platforms:

dependencies:
mineral_ioc: ^2.0.0
mineral_package: ^1.0.0
mineral: ^3.1.0
path: ^1.8.2
yaml: ^3.1.1
mineral_contract: ^1.1.0

dev_dependencies:
lints: ^2.0.0
Expand Down
22 changes: 10 additions & 12 deletions test/mineral_i18n_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:mineral_i18n/mineral_i18n.dart';
import 'package:mineral_i18n/src/mixins/translation.dart';
import 'package:mineral_ioc/ioc.dart';
import 'package:test/test.dart';

Expand All @@ -10,32 +9,31 @@ class Foo with Translation {
void main() {
final foo = Foo();
final String targetTranslation = 'foo.bar';
final i18n = I18n([Lang.fr, Lang.enGB])
final i18n = I18n(['fr', 'en'])
..registerLanguages();

i18n.translationManager
..addTranslations(Lang.fr, { targetTranslation: 'Salut {user}' })
..addTranslations(Lang.enGB, { targetTranslation: 'Hello {user}' });
..addTranslations('fr', { targetTranslation: 'Salut {user}' })
..addTranslations('en', { targetTranslation: 'Hello {user}' });

test('can register i18n into mineral ioc', () {
final dynamic pluginManager = ioc.services.entries.firstWhere((element) => element.key.toString() == 'PluginManagerCraft').value;
pluginManager.bind((ioc) => I18n([Lang.fr, Lang.enGB]));
ioc.bind((ioc) => I18n(['fr', 'en']));

expect(pluginManager.use(), equals(i18n));
expect(ioc.use<I18n>(), equals(i18n));
});

test('registered lang is two', () {
expect(i18n.languages.length, equals(2));
expect(i18n.languages.first, equals(Lang.fr));
expect(i18n.languages.first, equals('fr'));
});

test('can translate sentence without variables', () {
expect(foo.translator(Lang.fr, targetTranslation), equals('Salut {user}'));
expect(foo.translator(Lang.enGB, targetTranslation), equals('Hello {user}'));
expect(foo.translator('fr', targetTranslation), equals('Salut {user}'));
expect(foo.translator('en', targetTranslation), equals('Hello {user}'));
});

test('can translate sentence with variables', () {
expect(foo.translator(Lang.fr, targetTranslation, replacers: { 'user': 'Freeze' }), equals('Salut Freeze'));
expect(foo.translator(Lang.enGB, targetTranslation, replacers: { 'user': 'Freeze' }), equals('Hello Freeze'));
expect(foo.translator('fr', targetTranslation, replacers: { 'user': 'Freeze' }), equals('Salut Freeze'));
expect(foo.translator('en', targetTranslation, replacers: { 'user': 'Freeze' }), equals('Hello Freeze'));
});
}

0 comments on commit df35f12

Please sign in to comment.