Anyone tried the Translate service?

True, it should be a feature request.

Yes, you understood what I wanted to do.

Thanks guys!

I found another solution to translate the result of my pipe. In my first pipe, I return the string corresponding to my key and then translate it. Important, the return string needs to be between single quote, not double.

{{ test.id | testPipe | translate }}

Hello @icarus_31, you can use:
this.translate.instant(ā€˜mystringā€™);

Hi @danielbertini,

How did you get that ā€œinstantā€ function? I am running version 1.8.0 and donā€™t have it

Hello @icarus_31, i am using the same version 1.8.0, you implemented in the contructor method like this?

import {TranslateService, TranslatePipe} from "../../../../node_modules/ng2-translate";

@Page({
    templateUrl: 'build/pages/auth/signup/signup.html',
    pipes: [TranslatePipe],
    providers: [AuthService],
    directives: [FORM_DIRECTIVES]
})
export class Signup {
    contructor(private translate: TranslateService) {
        this.translate = translate;
        this.translate.instant('json.string');
    } 
}

Does the translate pipe work in IOS? iā€™m having trouble in IOS with the numeric pipe because IOS doesnā€™t support the internationalisation api, isnā€™t this api used by the translate pipe as well?

I donā€™t know, i not test on iOS yet.

If it doesnā€™t relate to this:

  1. https://github.com/angular/angular/issues/3333
  2. https://github.com/driftyco/ionic/issues/5710#issuecomment-192436323
  3. https://www.npmjs.com/package/intl

If using webpack the way i imported it is after installing the polyfill package put this in webpack.config.js:

module.exports = {
  entry: [
    path.normalize('es6-shim/es6-shim.min'),
    'reflect-metadata',
    path.normalize('zone.js/dist/zone-microtask'),
    'intl/index.js',
    'intl/locale-data/jsonp/en.js',
    path.resolve('app/app')
  ],

@danielbertini, when you start typeing ā€œthis.translate.ā€ do you see the ā€œinstantā€ function appears as an available function? I donā€™t, so this is why I was asking you that.

Here what I see:

image

Hey, you might want to try 1.9 we have a nice TRANSLATE_PROVIDERS that you can add to bootstrap now.
Also this should fix a bug with method instant throwing an error when the translation isnā€™t available.

If you need help, donā€™t hesitate to ask here or to open an issue on github.

Hi @ocombe

I tried to change my code, but now nothing is working. :frowning:

I had use useStaticFilesLoader before, but now it is no more valid.

I think I need to use the below, but donā€™t know where to add that code. Would you mind to help me on that one?

provide(TranslateLoader, {
    useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
    deps: [Http]
})

Here my code:

app.ts

constructor(..) {
    this._translate = translateService;
    this.translationConfig();
}

translationConfig() {
    let prefix = 'assets/i18n/';
    let suffix = '.json';
    
    this._translate.useStaticFilesLoader(prefix, suffix);

    var userLang = navigator.language.split('-')[0]; // use navigator lang if available
    userLang = /(fr|en)/gi.test(userLang) ? userLang : 'en';

    // optional, default is "en"
    this._translate.setDefaultLang('en');

    // the lang to use, if the lang isn't available, it will use the current loader to get them
    this._translate.use(userLang);
} 

Thanks

You need to inject that in the bootstrap of your application.
For Ionic 2 I think itā€™s the providers property of your @App component.
That would be something like that:

import {provide} from 'angular2/core';
import {TRANSLATE_PROVIDERS, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate';

@App({
  templateUrl: '....',
  config: {},
  providers: [
    provide(TranslateLoader, {
      useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
      deps: [Http]
    }),
    TRANSLATE_PROVIDERS
  ]
})

it canā€™t find ā€œprovideā€:

Cannot find name ā€˜provideā€™.

import {provide} from 'angular2/core';

Wow, learning lots of things today! :slightly_smiling:

Now, I have this:

http://localhost:8100/i18n/en.json 404 (Not Found)

Provide is used to configure providers by replacing one class with another for the dependency injection.
You can use it in unit tests, or when you change the location strategy for the router (to use hash instead of html 5 routes: https://angular.io/docs/ts/latest/api/router/HashLocationStrategy-class.html).
In this case we use it to change the loader (or to define the parameters other than the default parameters).

But anyway, it should call http://localhost:8100/assets/i18n/en.json and thatā€™s not the case.
I just checked and thereā€™s a bug, Iā€™ll fix it asap (within the hour).

Edit:
Ok my bad, itā€™s not a bug, itā€™s just that you donā€™t want to use TRANSLATE_PROVIDERS if you want to override the TranslateStaticLoader.

You should use:

import {provide} from 'angular2/core';
import {TranslateService, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate';

@App({
  templateUrl: '....',
  config: {},
  providers: [
    provide(TranslateLoader, {
      useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
      deps: [Http]
    }),
    TranslateService
  ]
})

Because TRANSLATE_PROVIDERS is already an instance of the static loader with the default params.

Yep! Working great!

Thanks

I facing SyntaxError with the code above. Refer the error code Row 15, Column 23 is belong to (http: Http)

Do you know the reason?

ERROR in ./app/app.js
Module build failed: SyntaxError: C:/Projects/apps/myapp/app/app.js: Unexpected token (15:23)
  providers: [
    provide(TranslateLoader, {
      useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
      deps: [Http]
    }),
    TranslateService

Below is my code

import {TranslateService, TranslateLoader, TranslateStaticLoader} from 'ng2-translate/ng2-translate';
import {provide} from 'angular2/core';

@App({
  templateUrl: 'build/app.html',
  config: {}, // http://ionicframework.com/docs/v2/api/config/Config/
  providers: [
    provide(TranslateLoader, {
      useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
      deps: [Http]
    }),
    TranslateService
  ]
})

Try to add TRANSLATE_PROVIDERS to ng2-translate.