Permalink
Browse files

feature: External programs are now a subclass of ShellCommand, struct…

…ure improved
  • Loading branch information...
MikeMitterer committed Aug 17, 2017
1 parent 7905f46 commit 3fc1c82104ea967fbd45a13ec0192405159d8941
View
@@ -12,6 +12,8 @@ $ pub global activate l10n
* msginit
* msgmerge
You can see the external commands with `mkl10nlocale -s`
(only if you want to generate PO/POT files)
To verify it they are on your system type:
@@ -24,7 +26,7 @@ If you get an error message - do the following:
# on Linux: apt-get install gettext
```
Edit ~/.bashrc and add:
Esurroundhrc and add:
```bash
# mkl10nlocale
export PATH=${PATH}:/usr/local/opt/gettext/bin
@@ -58,7 +60,7 @@ void main(List<String> arguments) {
// Uncomment in Step 3
// translate.locale = Intl.shortLocale(locale);
// Step 1 - sourround your text with l10n(...)
// Step 1 - surround your text with l10n(...)
// Try this: print(l10n("This is a test").message);
print(l10n("This is a test"));
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2017, 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 l10n.app;
abstract class ShellCommand {
/// Command name
final String name;
/// Full path to command
String _exeCache;
ShellCommand(this.name) {
Validate.notBlank(name);
}
String get executable {
if (_exeCache == null) {
_exeCache = whichSync(name);
}
return _exeCache;
}
Future<ProcessResult> run(List<String> arguments,
{String workingDirectory,
Map<String, String> environment,
bool includeParentEnvironment: true,
bool runInShell: false,
Encoding stdoutEncoding: SYSTEM_ENCODING,
Encoding stderrEncoding: SYSTEM_ENCODING}) =>
Process.run(executable, arguments,
workingDirectory: workingDirectory,
includeParentEnvironment: includeParentEnvironment,
runInShell: runInShell,
stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding);
runSync(List<String> arguments,
{String workingDirectory,
Map<String, String> environment,
bool includeParentEnvironment: true,
bool runInShell: false,
Encoding stdoutEncoding: SYSTEM_ENCODING,
Encoding stderrEncoding: SYSTEM_ENCODING}) =>
Process.runSync(executable, arguments,
workingDirectory: workingDirectory,
includeParentEnvironment: includeParentEnvironment,
runInShell: runInShell,
stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding);
// - private -------------------------------------------------------------------------------------------------------
}
View
@@ -0,0 +1,19 @@
part of l10n.app;
class MsgMerge extends ShellCommand {
MsgMerge() : super("msgmerge");
}
class MsgInit extends ShellCommand {
MsgInit() : super("msginit");
}
class XGetText extends ShellCommand {
XGetText() : super("xgettext");
}
final MsgMerge msgmerge = new MsgMerge();
final MsgInit msginit = new MsgInit();
final XGetText xgettext = new XGetText();
View
@@ -1,4 +1,4 @@
#!/usr/bin/env dart
library l10n.app;
import 'dart:io';
import 'dart:async';
@@ -7,6 +7,7 @@ import 'dart:convert';
import 'dart:math';
import 'package:args/args.dart';
import 'package:which/which.dart';
import 'package:intl/intl.dart';
import 'package:intl/intl_standalone.dart';
@@ -20,6 +21,9 @@ import 'package:validate/validate.dart';
import 'package:l10n/l10n.dart';
import 'package:l10n/locale/messages.dart';
part 'commands/ShellCommand.dart';
part 'commands/commands.dart';
class Application {
final Logger _logger = new Logger("mkl10llocale.Application");
@@ -67,7 +71,7 @@ class Application {
final File pofile = _preparePOFile(locale, potfile,config.getPOFile(locale));
_mergePO(pofile,potfile);
futuresForJson.add( _creatJson(locale,pofile).then((final Map<String,String> jsonForLocale) => json[locale] = jsonForLocale));
futuresForJson.add( _createJson(locale,pofile).then((final Map<String,String> jsonForLocale) => json[locale] = jsonForLocale));
});
Future.wait(futuresForJson).then((_) {
_createMergedJson(json,config.jsonfile);
@@ -123,9 +127,9 @@ class Application {
_logger.info("Merged-Json (${jsonFile.path}) created");
}
/// Updates your translated PO with new entries from .pot-File
/// Updates your translated PO with new records from .pot-File
void _mergePO(final File pofile,final File potfile) {
final ProcessResult result = Process.runSync('msgmerge', ['-U', pofile.path, potfile.path]);
final ProcessResult result = msgmerge.runSync(['-U', pofile.path, potfile.path]);
if(result.exitCode != 0) {
_logger.severe(result.stderr);
}
@@ -137,7 +141,7 @@ class Application {
final File pofile = new File(pofilename);
if(!pofile.existsSync()) {
pofile.createSync(recursive: true);
final ProcessResult result = Process.runSync('msginit', ['--no-translator','--input', potfile.path, '--output', pofile.path, '-l', locale ]);
final ProcessResult result = msginit.runSync(['--no-translator','--input', potfile.path, '--output', pofile.path, '-l', locale ]);
if(result.exitCode != 0) {
_logger.severe(result.stderr);
} else {
@@ -165,7 +169,7 @@ class Application {
_logger.fine(" -> ${file.path}");
// --from-code ... iconv -l shows all the available codes!
final ProcessResult result = Process.runSync('xgettext', ['-kl10n', '-kL10N', '-j', '-o', "$potfile", '-L', 'JavaScript','--from-code=utf-8', file.path ]);
final ProcessResult result = xgettext.runSync(['-kl10n', '-kL10N', '-j', '-o', "$potfile", '-L', 'JavaScript','--from-code=utf-8', file.path ]);
if (result.exitCode != 0) {
_logger.severe("${result.stderr}");
@@ -239,6 +243,18 @@ class Application {
settings.forEach((final String key,final String value) {
print(" ${prepareKey(key)} $value");
});
print("");
print(translate(l10n("External commands:")));
[ msgmerge, msginit, xgettext].forEach((final ShellCommand command) {
String exe = translate(l10n("not installed!"));
try {
exe = command.executable;
} on StateError catch(_) {}
print(" ${(command.name + ':').padRight(maxKeyLeght + 1)} ${exe}");
});
}
static ArgParser _createOptions() {
@@ -257,8 +273,8 @@ class Application {
}
/// Creates .json-File in the same location where the .po file is
Future<HashMap<String,String>> _creatJson(final String locale,final File pofile) {
final Completer<HashMap<String,String>> completer = new Completer<HashMap<String,String>>();
Future<HashMap<String,String>> _createJson(final String locale,final File pofile) {
final Completer<Map<String,String>> completer = new Completer<Map<String,String>>();
final Map<String,Map<String,String>> json = new HashMap<String,Map<String,String>>();
json[locale] = new HashMap<String,String>();
@@ -275,7 +291,7 @@ class Application {
// There is always a newline between the msg-blocks, so split there
final List<String> msgblocks = content.split(new RegExp("(\r\n|\n){2}"));
String _sanityze(final String value) {
String _sanitize(final String value) {
return value.replaceFirst("msgid","").trim().replaceFirst(new RegExp("^\""),"").replaceFirst(new RegExp('"\$'),"").trim();
}
// If there is a header - skip it!
@@ -284,8 +300,8 @@ class Application {
final String withoutcomment = block.replaceAll(new RegExp("#.*(\r\n|\n)"),"");
final List<String> message = withoutcomment.split("msgstr");
final String key = _sanityze(message[0].replaceFirst("msgid",""));
final String value = _sanityze(message[1]);
final String key = _sanitize(message[0].replaceFirst("msgid",""));
final String value = _sanitize(message[1]);
json[locale][key] = value;
});
View
@@ -45,6 +45,7 @@ abstract class Translatable {
/// bind(Translator, toValue: _translator);
/// }
/// }
@di.injectable
abstract class Translator {
String translate(final L10N l10n);
View
@@ -4,13 +4,14 @@ import "dart:collection";
import 'package:logging/logging.dart';
import 'dart:convert';
import 'package:intl/intl.dart';
import 'package:dice/dice.dart' as di;
import "package:validate/validate.dart";
part "interfaces.dart";
part 'src/l10n/L10NImpl.dart';
part 'src/l10n/L10NTranslate.dart';
part 'l10n/L10NImpl.dart';
part 'l10n/L10NTranslate.dart';
final L10NTranslate TRANSLATOR = new L10NTranslate();
File renamed without changes.
File renamed without changes.
View
@@ -9,33 +9,38 @@ import 'package:l10n/l10n.dart';
final L10NTranslate translate = new L10NTranslate.withTranslations( {
"en": {
"Usage: mkl10nlocale [options] <dir(s) to scan>": "Usage: mkl10nlocale [options] <dir(s) to scan>",
"Libprefix for generated DART-File (library <prefix>.locale;)": "Libprefix for generated DART-File (library <prefix>.locale;)",
"Defines where to place your locale-Dir": "Defines where to place your locale-Dir",
"Dirs to scan": "Dirs to scan",
"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.": "",
"Settings:": "Settings:",
"locales - separated by colon, Sample: --locales en,de,es": "locales - separated by colon, Sample: --locales en,de,es",
"Shows this message": "Shows this message",
"Der Server meldet {{statuscode-401}} bei der API-Key Anforderung.": "",
"Where should the DART-File go? (<path>/locale/messages.dart)": "Where should the DART-File go? (<path>/locale/messages.dart)",
"Ein TEST - 290714 1648": "",
"Prints settings": "Prints settings"
},
"de": {
"Usage: mkl10nlocale [options] <dir(s) to scan>": "Verwendung: mkl10nlocale [options] <dir(s) to scan>",
"Libprefix for generated DART-File (library <prefix>.locale;)": "Library-Prefix für das generierte DART-File (library <prefix>.locale;)",
"\"\n\"Hallo,\\n\"\n\" dies ist ein {{what}}": "",
"Defines where to place your locale-Dir": "Definiert Basisverzeichnis für das locale-Directory",
"Dirs to scan": "Directory dass durchsucht werden soll",
"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.": "Fehlerhafte Anfrage (400) bei der API-Key Anforderung!",
"Settings:": "",
"Mike + {{name}}": "",
"\"\n\"Hallo\\n\"\n\"Test": "",
"This is a test": "",
"Hallo Mike II": "",
"Hallo {{?}}": "",
"Usage: mkl10nlocale [options] <dir(s) to scan>": "Verwendung: mkl10nlocale [options] <dir(s) to scan>",
"Hallo {{l10n}}": "",
"locales - separated by colon, Sample: --locales en,de,es": "locales - getrennt durch Beistrich, Sample: --locales en,de,es",
"Shows this message": "Zeigt diese Message an",
"Der Server meldet {{statuscode-401}} bei der API-Key Anforderung.": "",
"Hallo {{name}}, du bist jetzt {{age}} Jahre alt": "",
"Hallo Mike": "",
"Libprefix for generated DART-File (library <prefix>.locale;)": "Library-Prefix für das generierte DART-File (library <prefix>.locale;)",
"not installed!": "",
"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.": "Fehlerhafte Anfrage (400) bei der API-Key Anforderung!",
"External commands:": "Externe programme:",
"Test1": "",
"Hallo, dies ist ein {{what}}": "",
"Dirs to scan": "Directory dass durchsucht werden soll",
"Shows this message": "Zeigt diese Message an",
"Hello {{what}}": "",
"RTL": "",
"Hallo, {{name}}": "",
"LTR": "",
"Where should the DART-File go? (<path>/locale/messages.dart)": "Wo soll das DART-File generiert werden? (<pfad>/locale/messages.dart)",
"Ein TEST - 290714 1648": "",
"Prints settings": "Zeigt die aktuellen settings an"
"Prints settings": "Zeigt die aktuellen settings an",
"UNKNOWN": "",
"Hallo {{name}}": ""
}
});
@@ -1,16 +1,35 @@
{
"de": {
"Usage: mkl10nlocale [options] <dir(s) to scan>": "Verwendung: mkl10nlocale [options] <dir(s) to scan>",
"Libprefix for generated DART-File (library <prefix>.locale;)": "Library-Prefix für das generierte DART-File (library <prefix>.locale;)",
"\"\n\"Hallo,\\n\"\n\" dies ist ein {{what}}": "",
"Defines where to place your locale-Dir": "Definiert Basisverzeichnis für das locale-Directory",
"Dirs to scan": "Directory dass durchsucht werden soll",
"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.": "Fehlerhafte Anfrage (400) bei der API-Key Anforderung!",
"Settings:": "",
"Mike + {{name}}": "",
"\"\n\"Hallo\\n\"\n\"Test": "",
"This is a test": "",
"Hallo Mike II": "",
"Hallo {{?}}": "",
"Usage: mkl10nlocale [options] <dir(s) to scan>": "Verwendung: mkl10nlocale [options] <dir(s) to scan>",
"Hallo {{l10n}}": "",
"locales - separated by colon, Sample: --locales en,de,es": "locales - getrennt durch Beistrich, Sample: --locales en,de,es",
"Shows this message": "Zeigt diese Message an",
"Der Server meldet {{statuscode-401}} bei der API-Key Anforderung.": "",
"Hallo {{name}}, du bist jetzt {{age}} Jahre alt": "",
"Hallo Mike": "",
"Libprefix for generated DART-File (library <prefix>.locale;)": "Library-Prefix für das generierte DART-File (library <prefix>.locale;)",
"not installed!": "",
"Der Server meldet {{statuscode-400}} bei der API-Key Anforderung.": "Fehlerhafte Anfrage (400) bei der API-Key Anforderung!",
"External commands:": "Externe programme:",
"Test1": "",
"Hallo, dies ist ein {{what}}": "",
"Dirs to scan": "Directory dass durchsucht werden soll",
"Shows this message": "Zeigt diese Message an",
"Hello {{what}}": "",
"RTL": "",
"Hallo, {{name}}": "",
"LTR": "",
"Where should the DART-File go? (<path>/locale/messages.dart)": "Wo soll das DART-File generiert werden? (<pfad>/locale/messages.dart)",
"Ein TEST - 290714 1648": "",
"Prints settings": "Zeigt die aktuellen settings an"
"Prints settings": "Zeigt die aktuellen settings an",
"UNKNOWN": "",
"Hallo {{name}}": ""
}
}
Oops, something went wrong.

0 comments on commit 3fc1c82

Please sign in to comment.