Skip to content

Commit 22f2038

Browse files
committed
feat(parser): allow using custom parser
1 parent 62ac73b commit 22f2038

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+917
-555
lines changed

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ import {TranslateService} from 'ng2-translate';
121121
@Component({
122122
selector: 'app',
123123
template: `
124-
<div>{{ 'HELLO' | translate:{value: param} }}</div>
124+
<div>{{ 'HELLO' | translate:param }}</div>
125125
`
126126
})
127127
export class AppComponent {
128-
param: string = 'world';
128+
param = {value: 'world'};
129129

130130
constructor(translate: TranslateService) {
131131
// this language will be used as a fallback when a translation isn't found in the current language
@@ -167,11 +167,11 @@ The `TranslateParser` understands nested JSON objects. This means that you can h
167167

168168
You can then access the value by using the dot notation, in this case `HOME.HELLO`.
169169

170-
#### 4. Use the service or the pipe:
170+
#### 4. Use the service, the pipe or the directive:
171171

172-
You can either use the `TranslateService` or the `TranslatePipe` to get your translation values.
172+
You can either use the `TranslateService`, the `TranslatePipe` or the `TranslateDirective` to get your translation values.
173173

174-
With the service, it looks like this.
174+
With the **service**, it looks like this:
175175

176176
```ts
177177
translate.get('HELLO', {value: 'world'}).subscribe((res: string) => {
@@ -180,10 +180,25 @@ translate.get('HELLO', {value: 'world'}).subscribe((res: string) => {
180180
});
181181
```
182182

183-
And this is how you do it with the pipe.
183+
This is how you do it with the **pipe**:
184184

185185
```html
186-
<div>{{ 'HELLO' | translate:{value: param} }}</div>
186+
<div>{{ 'HELLO' | translate:param }}</div>
187+
```
188+
189+
And in your component define `param` like this:
190+
```ts
191+
param = {value: 'world'};
192+
```
193+
194+
This is how you use the **directive**:
195+
```html
196+
<div [translate]="'HELLO'" [translateparams]="{param: 'world'}"></div>
197+
```
198+
199+
Or even simpler using the content of your element as a key:
200+
```html
201+
<div translate [translateparams]="{param: 'world'}">HELLO</div>
187202
```
188203

189204
#### 5. Use HTML tags:
@@ -196,7 +211,7 @@ You can easily use raw HTML tags within your translations.
196211
}
197212
```
198213

199-
To render them, simply use the `innerHTML` attributeon any element.
214+
To render them, simply use the `innerHTML` attribute with the pipe on any element.
200215

201216
```html
202217
<div [innerHTML]="'HELLO' | translate"></div>
@@ -236,7 +251,7 @@ To render them, simply use the `innerHTML` attributeon any element.
236251
- `setTranslation(lang: string, translations: Object, shouldMerge: boolean = false)`: Manually sets an object of translations for a given language, set `shouldMerge` to true if you want to append the translations instead of replacing them
237252
- `addLangs(langs: Array<string>)`: Add new langs to the list
238253
- `getLangs()`: Returns an array of currently available langs
239-
- `get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Gets the translated value of a key (or an array of keys)
254+
- `get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Gets the translated value of a key (or an array of keys) or the key if the value was not found
240255
- `instant(key: string|Array<string>, interpolateParams?: Object): string|Object`: Gets the instant translated value of a key (or an array of keys). /!\ This method is **synchronous** and the default file loader is asynchronous. You are responsible for knowing when your translations have been loaded and it is safe to use this method. If you are not sure then you should use the `get` method instead.
241256
- `set(key: string, value: string, lang?: string)`: Sets the translated value of a key
242257
- `reloadLang(lang: string): Observable<string|Object>`: Calls resetLang and retrieves the translations object for the current loader
@@ -310,7 +325,7 @@ export class AppModule { }
310325

311326
### Parser
312327

313-
If you need it for some reason, you can use the `TranslateParser` service.
328+
If you need it for some reason, you can use the `DefaultParser` service.
314329

315330
#### Methods:
316331
- `interpolate(expr: string, params?: any): string`: Interpolates a string to replace parameters.
@@ -344,6 +359,7 @@ Ionic 2 is still using angular 2 RC4, but ng2-translate uses RC5. You should fix
344359

345360
## Plugins
346361
- [Localize Router](https://github.com/Greentube/localize-router) by @meeroslav: An implementation of routes localization for Angular 2. If you need localized urls (for example /fr/page and /en/page).
362+
- [.po files Loader](https://www.npmjs.com/package/@biesbjerg/ng2-translate-po-loader) by @biesbjerg: Use .po translation files with ng2-translate
347363

348364

349365
## Additional Framework Support

config/karma.conf.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function(config) {
77
frameworks: ['jasmine'],
88

99
// list of files to exclude
10-
exclude: [ ],
10+
exclude: [],
1111

1212
/*
1313
* list of files / patterns to load in the browser
@@ -36,6 +36,10 @@ module.exports = function(config) {
3636

3737
reporters: [ 'mocha', 'coverage', 'remap-coverage' ],
3838

39+
mochaReporter: {
40+
ignoreSkipped: true
41+
},
42+
3943
// web server port
4044
port: 9876,
4145

examples/ionic2/.gitignore

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1-
# Specifies files to intentionally ignore when using Git
1+
# Specifies intentionally untracked files to ignore when using Git
22
# http://git-scm.com/docs/gitignore
33

4+
*~
5+
*.sw[mnpcod]
6+
*.log
7+
*.tmp
8+
*.tmp.*
9+
log.txt
10+
*.sublime-project
11+
*.sublime-workspace
12+
.vscode/
13+
npm-debug.log*
14+
15+
.idea/
16+
.sass-cache/
17+
.tmp/
18+
.versions/
19+
coverage/
20+
dist/
421
node_modules/
5-
www/build/
22+
tmp/
23+
temp/
24+
hooks/
625
platforms/
726
plugins/
8-
*.swp
27+
plugins/android.json
28+
plugins/ios.json
29+
www/
30+
$RECYCLE.BIN/
31+
932
.DS_Store
1033
Thumbs.db
34+
UserInterfaceState.xcuserstate

examples/ionic2/app/app.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/ionic2/app/pages/about/about.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/ionic2/app/pages/about/about.scss

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/ionic2/app/pages/about/about.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/ionic2/app/pages/contact/contact.scss

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/ionic2/app/pages/contact/contact.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/ionic2/app/pages/home/home.scss

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)