diff --git a/lib/mineral_i18n.dart b/lib/mineral_i18n.dart index b6887d5..10ee6b8 100644 --- a/lib/mineral_i18n.dart +++ b/lib/mineral_i18n.dart @@ -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'; diff --git a/lib/src/contracts/i18n_contract.dart b/lib/src/contracts/i18n_contract.dart index 63fb21d..8ce1782 100644 --- a/lib/src/contracts/i18n_contract.dart +++ b/lib/src/contracts/i18n_contract.dart @@ -1,8 +1,6 @@ import 'dart:io'; -import 'package:mineral_i18n/src/lang.dart'; - abstract class I18nContract { - List get languages; + List get languages; Directory get langPath; } diff --git a/lib/src/i18n.dart b/lib/src/i18n.dart index f2b57b4..e8d1102 100644 --- a/lib/src/i18n.dart +++ b/lib/src/i18n.dart @@ -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 _languages; + + final List _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 allowedLanguages = i18n.languages; /// ``` @override - List get languages => _languages; + List get languages => _languages; /// ## Languages root directory @@ -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 init () async { @@ -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; diff --git a/lib/src/lang.dart b/lib/src/lang.dart deleted file mode 100644 index 1834162..0000000 --- a/lib/src/lang.dart +++ /dev/null @@ -1,43 +0,0 @@ -enum Lang { - da('da', null), - de('de', null), - enGB('en-GB', 'en'), - enUS('en-US', 'en'), - esES('es-ES', 'es'), - fr('fr', null), - hr('hr', null), - it('it', null), - lt('lt', null), - hu('hu', null), - nl('nl', null), - no('no', null), - pl('pl', null), - ptBR('pt-BR', 'pt'), - ro('ro', null), - fi('fi', null), - svSE('sv-SE', 'sv'), - vi('vi', null), - tr('tr', null), - cs('cs', null), - el('el', null), - bg('bg', null), - ru('ru', null), - uk('uk', null), - hi('hi', null), - th('th', null), - zhCN('zh-CN', 'zh'), - ja('ja', null), - zhTW('zh-TW', null), - ko('ko', null); - - final String _locale; - final String? _normalize; - - const Lang(this._locale, this._normalize); - - String get locale => _locale; - String get normalize => _normalize ?? _locale; - - @override - String toString() => locale; -} diff --git a/lib/src/translation_manager.dart b/lib/src/managers/translation_manager.dart similarity index 74% rename from lib/src/translation_manager.dart rename to lib/src/managers/translation_manager.dart index 8e113a3..6517312 100644 --- a/lib/src/translation_manager.dart +++ b/lib/src/managers/translation_manager.dart @@ -1,13 +1,11 @@ -import 'package:mineral_i18n/src/lang.dart'; - class TranslationManager { final Map _cache = {}; Map get cache => _cache; - void addTranslations (Lang lang, Map translations) { + void addTranslations (final String lang, Map translations) { for (final translation in translations.entries) { - dynamic location = _cache[lang.normalize]; + dynamic location = _cache[lang] ?? {}; List keys = translation.key.split('.'); for (final key in keys) { diff --git a/lib/src/mixins/translation.dart b/lib/src/mixins/translation.dart index f237724..d3887f8 100644 --- a/lib/src/mixins/translation.dart +++ b/lib/src/mixins/translation.dart @@ -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(); - } - /// Translates the sentence defined by the key set into the requested language. /// Replacement parameters can be injected. /// ```dart @@ -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? replacers }) { - dynamic target = _getPlugin().translationManager.cache[lang.normalize]; + String t (String lang, String key, { Map? replacers }) { + dynamic target = ioc.use().translationManager.cache[lang]; for (final element in key.split('.')) { target = target[element]; } diff --git a/pubspec.lock b/pubspec.lock index cd7f34c..fea476b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: @@ -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: @@ -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: @@ -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: @@ -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" diff --git a/pubspec.yaml b/pubspec.yaml index f77eca9..a016404 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/mineral_i18n_test.dart b/test/mineral_i18n_test.dart index f901b45..fda63ce 100644 --- a/test/mineral_i18n_test.dart +++ b/test/mineral_i18n_test.dart @@ -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'; @@ -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(), 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')); }); }