Skip to content

Commit

Permalink
doc: cmdline-Sample added
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMitterer committed Aug 23, 2017
1 parent 6008eda commit 406e9a5
Show file tree
Hide file tree
Showing 19 changed files with 499 additions and 149 deletions.
2 changes: 1 addition & 1 deletion .mkl10n.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Ignore these dirs
# exclude_dirs: example, test
exclude_dirs: samples
165 changes: 35 additions & 130 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,162 +11,72 @@ $ pub global activate l10n
```

### System requirements
Install the following cmdline-Applications:
* xgettext
* 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:
```bash
$ type xgettext
mkl10n -s
```
If you get an error message - do the following:
```bash
$ brew install gettext
# on Linux: apt-get install gettext
```

Esurroundhrc and add:
```bash
# mkl10nlocale
export PATH=${PATH}:/usr/local/opt/gettext/bin
```

To activate your settings:
```bash
$ source ~/.bashrc
```

### How to use it
[![Screenshot][1])](http://www.youtube.com/watch?v=vPfl-xPTjs0)
(You have to watch it in 1080p - sorry! Better screencast will follow)

**Sample code**
```dart
import 'package:intl/intl.dart';
import 'package:intl/intl_standalone.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:l10n/l10n.dart';
// This file will be generated by the framework (l10n)
// Uncomment in Step 3
//import 'package:<your package>/locale/messages.dart';
void main(List<String> arguments) {
final Application application = new Application();
findSystemLocale().then((final String locale) {
// Uncomment in Step 3
// translate.locale = Intl.shortLocale(locale);
// Step 1 - surround your text with l10n(...)
// Try this: print(l10n("This is a test").message);
print(l10n("This is a test"));
// Step 2 - run mkl10nlocale -l de,en example/
// Step 3 - add the import-statement for locale/messages.dart
// + set the locale
// Step 4 - add 'translate' to your print statement
print(translate(l10n("This is a test")));
// Step 5 - Translate the entry in your
// PO (for example local/de/messages.po
// Step 6 - run mkl10nlocale -l de,en
// Step 7 - run your program
});
}
```
- Download the example from `samples/cmdline`
- Run `pub update`
- Run `mkl10n .`
Generates the required .po,.pot files and the lib/locale/messages.dart
- Translate the generated .po-File (`locale/<your localr>/messages.po`)
- Run `mkl10n .` again

Run `dart bin/cmdline.dart -s` - you should see the translated strings

```bash
$ dart mini.dart
SystemLocale: de_AT
Dies ist ein TEST!
```
Play with `dart bin/cmdline.dart -l de -s` and `dart bin/cmdline.dart -l en -s`

### How to use it with AngularDart
- Write your Filter for Angular
**This is the most important code-part:**
_cmdline/Config.dart_

```dart
library your.lib;
import 'package:logging/logging.dart';
Map<String,String> get settings {
final Map<String,String> settings = new Map<String,String>();
import "package:angular/angular.dart";
import 'package:angular/core/annotation_src.dart';
// Everything within l10n(...) will be in your .po File
settings[translate(l10n("loglevel"))] = loglevel;
import 'package:l10n/l10n.dart';
// 'translate' will translate your ID/String
settings[translate(l10n("Config folder"))] = configfolder;
settings[translate(l10n("Config file"))] = configfile;
settings[translate(l10n("Locale"))] = locale;
/// Filter for L10N
@Formatter(name: "translate")
class TranslateFilter implements Function {
final _logger = new Logger('your.lib.TranslateFilter');
final L10NTranslate _translator;
if(dirstoscan.length > 0) {
settings[translate(l10n("Dirs to scan"))] = dirstoscan.join(", ");
}
TranslateFilter(this._translator) {
Validate.notNull(_translator);
return settings;
}
dynamic call(l10n) {
if (l10n is L10N) {
return _translator(l10n);
}
else {
return l10n;
}
}
}
```

- Implement it into your main.dart

```dart
library your.main.lib
import 'package:angular/angular.dart';
import 'package:angular/application_factory.dart';
import 'package:l10n/l10n.dart';
//---------------------------------------------------------
// Filter
import 'package:webapp_base_ui/angular/filter/TranslateFilter.dart';
/// Entry point into app.
main() {
applicationFactory().addModule(new SampleModule()).run();
}
/// only for this sample - usually use the Map from lib/locale/messages.dart
final L10NTranslate _translater = new L10NTranslate.withTranslation( {
"Could not find Job-ID: {{jobid}}" : "Konnte die JOB-ID {{jobid}} nicht finden..."
});
### How to use it with Material Design 4 Dart

class SampleModule extends Module {
SampleModule() {
// -- filter
bind(TranslateFilter,toValue: new TranslateFilter(_translater));
}
}
```

- Your HTML-Template (Component in this case) in Angular
Check out this sample on GitHub:
- [mdld_translate](https://github.com/MikeMitterer/dart-material-design-lite-site/tree/master/samples/mdld_translate)

This sample also shows the usage with Dice - the dependency injection framework

HTML-Translation: (_index.html_)
```html
<!DOCTYPE html>
<div ng-if="cmp.job != null">
ID: {{cmp.id}} {{cmp.job.description}}
</div>
<div ng-if="cmp.job == null">
{{cmp.lasterror | translate}}
</div>
<!-- /* Comment added from HTML-File */ -->
<span translate>_('Translate me')</span>
```

#### Sub-Translations
Expand Down Expand Up @@ -213,17 +123,12 @@ The rest is done be the nice merging-feature of l10n/msgmerge
### If you have problems
* [Issues][2]

### History
* 0.15.12 - toJson converts vars not to "encodable" values
* 0.11.0 - Sub-Translations are possible, msginit gets initialized with utf-8 per default
* 0.9.0 - Released on pub

## Links
- [GNU gettext utilities](https://www.gnu.org/software/gettext/manual/gettext.html)

### License

Copyright 2016 Michael Mitterer (office@mikemitterer.at),
Copyright 2017 Michael Mitterer (office@mikemitterer.at),
IT-Consulting and Development Limited, Austrian Branch

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
18 changes: 0 additions & 18 deletions example/mini.dart

This file was deleted.

2 changes: 2 additions & 0 deletions samples/cmdline/.mkl10n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore these dirs
exclude_dirs: example, test, samples
5 changes: 5 additions & 0 deletions samples/cmdline/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.0.1

- Initial version, created by StageDive
24 changes: 24 additions & 0 deletions samples/cmdline/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2017, Mike Mitterer.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 3 additions & 0 deletions samples/cmdline/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# stagedive

A minimal command-line application.
25 changes: 25 additions & 0 deletions samples/cmdline/bin/cmdline.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:async';
import 'package:intl/intl.dart';
import 'package:intl/intl_standalone.dart';
import 'package:intl/date_symbol_data_local.dart';

import 'package:l10n_sample_cmdline/cmdline.dart';
import 'package:l10n_sample_cmdline/locale/messages.dart';


Future main(List<String> arguments) async {

// Determine your locale automatically:
final String locale = await findSystemLocale();
translate.locale = Intl.shortLocale(locale);

// Avoids error message:
// LocaleDataException: Locale data has not been initialized, call initializeDateFormatting(<locale>).
await initializeDateFormatting(locale);

// For debugging
// print("SystemLocale: $locale"); // in my case: de_AT.UTF-8

final Application application = new Application();
application.run( arguments, locale );
}
26 changes: 26 additions & 0 deletions samples/cmdline/lib/cmdline.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

/// This is an awesome library. More dartdocs go here.
library cmdline;

import 'dart:io';
import 'dart:math';

import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:logging_handlers/logging_handlers_shared.dart';

import 'package:validate/validate.dart';
import "package:yaml/yaml.dart" as yaml;

import 'package:args/args.dart';

import 'package:l10n/l10n.dart';

// This file will be generated by the framework (l10n / 'mkl10n .')
import 'package:l10n_sample_cmdline/locale/messages.dart';

part "cmdline/Application.dart";
part "cmdline/Config.dart";
part "cmdline/Options.dart";
Loading

0 comments on commit 406e9a5

Please sign in to comment.