Permalink
Browse files

doc: More documentation for translate + sample

  • Loading branch information...
MikeMitterer committed Dec 16, 2015
1 parent 362aefd commit 3f0445b8a58bb2095b41e87c7419cafa687ed4d6
View
@@ -29,6 +29,22 @@ abstract class Translatable {
/// Basisklasse / Interface für den Translator.
/// call wird gleich hier implementiert.
///
/// final L10NTranslate _translator = new L10NTranslate.withTranslations( <String,Map<String,String>> {
/// "en" : {
/// "Could not find Job-ID: {{jobid}}" : "Could not find Job-ID: {{jobid}}"
/// },
///
/// "de" : {
/// "Could not find Job-ID: {{jobid}}" : "Konnte die JOB-ID {{jobid}} nicht finden..."
/// }
/// });
///
/// class SampleModule extends di.Module {
/// SampleModule() {
/// bind(Translator, toValue: _translator);
/// }
/// }
abstract class Translator {
String translate(final L10N l10n);
@@ -51,29 +51,54 @@ class L10NTranslate extends Translator {
/// Wenn es einen Eintrag für message.key in der _locale Tabelle gibt wir
/// ein String.replaceAll auf die Variablen in der message ausgeführt
///
/// final int status = 400;
/// final L10N l = new L10N(
/// """
/// Der Server meldet {{status}} bei der API-Key Anforderung.
/// """, {
/// "status" : "{{statuscode-${status}}}"
/// });
///
/// Result:
///
/// expect(l.message,"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.");
/// expect(translate(l),"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.");
///
/// translate.locale = "de";
/// expect(translate(l),"Fehlerhafte Anfrage (400) bei der API-Key Anforderung!");
///
String translate(final L10N l10n) {
Validate.notNull(l10n);
String _replaceVarsInMessage(final Map<String,dynamic> vars,final String msgid) {
/// From the above sample this translates {{status}} -> {{statuscode-400}} which results
/// in 'Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.'
String _replaceSubVarsInMessage(final Map<String,dynamic> vars,final String msgid) {
String translated = msgid;
vars.forEach((final String key,final value) {
translated = translated.replaceAll("{{$key}}",value.toString());
if(value is String && value.startsWith("{{") && value.endsWith("}}")) {
translated = translated.replaceAll("{{$key}}",value.toString());
}
});
return translated;
}
String message = _getMessage(l10n.msgid);
message = _replaceVarsInMessage(l10n.vars,message);
/// Translates all vars ({{varname}}) in message
String _replaceVarsInMessage(final Map<String,dynamic> vars,final String message) {
String translated = message;
// maybe _replaceVarsInMessage produced a new msgid...
// could be: Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.
// If so - try to translate again!
if(message.contains(new RegExp("{{.*}}"))) {
message = _getMessage(message);
vars.forEach((final String key,final value) {
translated = translated.replaceAll("{{$key}}",value.toString());
});
return translated;
}
return message;
// Translate e.g. 'Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.'
// to 'The server response with {{statuscode-400}} on the API-Key request.'
final String message = _getMessage(_replaceSubVarsInMessage(l10n.vars,l10n.msgid));
return _replaceVarsInMessage(l10n.vars,message);
}
String get locale => _locale;
@@ -123,6 +148,7 @@ class L10NTranslate extends Translator {
message = _translations[testLocale][msgid];
return true;
}
_logger.warning("No translation found for (locale: '$testLocale'): $msgid");
return false;
});
} on ArgumentError {
@@ -1,4 +1,3 @@
@TestOn("content-shell")
import 'package:test/test.dart';
import 'package:logging/logging.dart';
@@ -1,4 +1,3 @@
@TestOn("content-shell")
import 'package:test/test.dart';
import 'package:l10n/l10n.dart';
View
@@ -13,7 +13,7 @@ testUnit() {
new TestRunner().testAsync(files: "test/unit");
// Alle test mit @TestOn("content-shell") im header
new TestRunner().test(files: "test/unit",platformSelector: "content-shell");
// new TestRunner().test(files: "test/unit",platformSelector: "content-shell");
}
@Task()

0 comments on commit 3f0445b

Please sign in to comment.