Permalink
Browse files

feature: Translation-Component added (MaterialTranslate)

  • Loading branch information...
MikeMitterer committed Aug 22, 2017
1 parent 1b48d83 commit 6b5e0fee6dc734d17cf127f48541bc6439b522c0
@@ -29,6 +29,7 @@ import 'dart:collection';
import 'package:logging/logging.dart';
import 'package:validate/validate.dart';
import 'package:dice/dice.dart' as di;
import 'package:l10n/l10n.dart';

import 'package:mdl/mdlcore.dart';
import 'package:mdl/mdlcomponents.dart';
@@ -40,13 +41,14 @@ import 'package:mustache/mustache.dart';
//import 'package:mdl/mustache.dart';


part "src/directive/components/MaterialObserve.dart";
part "src/directive/components/MaterialModel.dart";
part "src/directive/components/MaterialClass.dart";
part "src/directive/components/MaterialAttribute.dart";
part "src/directive/components/MaterialClass.dart";
part "src/directive/components/MaterialModel.dart";
part "src/directive/components/MaterialObserve.dart";
part "src/directive/components/MaterialTranslate.dart";

part "src/directive/components/model/ModelObserverFactory.dart";
part "src/directive/components/model/ModelObserver.dart";
part "src/directive/components/model/ModelObserverFactory.dart";

part "src/directive/utils.dart";

@@ -61,10 +63,11 @@ final MdlDirectiveModule _directiveModule = new MdlDirectiveModule();

void registerMdlDirectiveComponents() {

registerMaterialObserve();
registerMaterialModel();
registerMaterialClass();
registerMaterialAttribute();
registerMaterialClass();
registerMaterialModel();
registerMaterialObserve();
registerMaterialTranslate();

componentHandler().addModule(_directiveModule);

@@ -0,0 +1,102 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of mdldirective;

/// Translate
///
/// https://angular.io/guide/i18n
/// http://www.labri.fr/perso/fleury/posts/programming/a-quick-gettext-tutorial.html
///
@Component
class MaterialTranslate extends MdlComponent {
final Logger _logger = new Logger('mdldirective.MaterialTranslate');

//static const _MaterialTranslateConstant _constant = const _MaterialTranslateConstant();
static const _MaterialTranslateCssClasses _cssClasses = const _MaterialTranslateCssClasses();

String _idToTranslate = "";
final Translator translator;

MaterialTranslate.fromElement(final dom.HtmlElement element,final di.Injector injector)
: translator = injector.get(Translator), super(element,injector) {

_init();
}

static MaterialTranslate widget(final dom.HtmlElement element) => mdlComponent(element,MaterialTranslate) as MaterialTranslate;


//- private -----------------------------------------------------------------------------------

void _init() {
_logger.info("MaterialTranslate - init");

/// Recommended - add SELECTOR as class
element.classes.add(_MaterialTranslateConstant.WIDGET_SELECTOR);

final String translation = element.text.replaceFirstMapped(
new RegExp('(_|l10n|L10N)\\((\'|\")([^\"\']*)(\'|\")\\)'),
(final Match match) {
_idToTranslate = match.group(3).trim();
return translator.translate(new L10N(_idToTranslate));
});

_logger.info("-> " + _idToTranslate);

if(_idToTranslate.isNotEmpty) {
element.text = translation;
}

element.classes.add(_cssClasses.IS_UPGRADED);
}

String get _fieldname => element.attributes[_MaterialTranslateConstant.WIDGET_SELECTOR].trim();
}

/// registration-Helper
void registerMaterialTranslate() {
final MdlConfig config = new MdlConfig<MaterialTranslate>(
_MaterialTranslateConstant.WIDGET_SELECTOR,
(final dom.HtmlElement element,final di.Injector injector) => new MaterialTranslate.fromElement(element,injector)
);

// If you want <mdl-model></mdl-model> set selectorType to SelectorType.TAG.
// If you want <div mdl-model></div> set selectorType to SelectorType.ATTRIBUTE.
// By default it's used as a class name. (<div class="mdl-model"></div>)
config.selectorType = SelectorType.ATTRIBUTE;

componentHandler().register(config);
}

/// Store strings for class names defined by this component that are used in
/// Dart. This allows us to simply change it in one place should we
/// decide to modify at a later date.
class _MaterialTranslateCssClasses {

final String IS_UPGRADED = 'is-upgraded';

const _MaterialTranslateCssClasses(); }

/// Store constants in one place so they can be updated easily.
class _MaterialTranslateConstant {

static const String WIDGET_SELECTOR = "translate";

const _MaterialTranslateConstant();
}
@@ -35,6 +35,9 @@ dependencies:

path: ^1.4.0

l10n: # ^0.17.0
path: /Volumes/Daten/DevLocal/DevDart/L10N4Dart

dev_dependencies:
test: any
args: any
@@ -0,0 +1,47 @@
@TestOn("content-shell")
import 'package:test/test.dart';

import 'dart:html' as dom;

import "package:mdl/mdl.dart";

// import 'package:logging/logging.dart';

import '../../config.dart';

main() async {
// final Logger _logger = new Logger("test.Formatter.Button");

configLogging();

final DomRenderer renderer = new DomRenderer();
final dom.DivElement parent = new dom.DivElement();
final String html = '''
<div>
<!-- /* Hallo Kommentar aus HTML */ -->
<span translate>_('Angular way8')</span>
</div>
'''.trim().replaceAll(new RegExp(r"\s+")," ");

group('Translate', () {
setUp(() async {
await prepareMdlTest( () async {
await registerMaterialTranslate();
});
});

test('> Translate', () async {
final dom.HtmlElement element = await renderer.render(parent,html);

await componentHandler().upgradeElement(element);
//final MaterialTranslate divTranslate = MaterialTranslate.widget(element);
//expect(divTranslate,isNotNull);

}); // end of 'Uppercase' test

});
// End of 'Formatter' group
}

// - Helper --------------------------------------------------------------------------------------

0 comments on commit 6b5e0fe

Please sign in to comment.