Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please define another method for Instant string retrieval #20

Closed
lordgreg opened this issue Jan 27, 2016 · 7 comments
Closed

Please define another method for Instant string retrieval #20

lordgreg opened this issue Jan 27, 2016 · 7 comments

Comments

@lordgreg
Copy link

angular-translate had such a good method which you could use in JS (not in templates) without getting a Promise back.

$translate.instant('key') would then return you your value of key in lang file.

where as current translate (from ng2-translate) always returns observable. There are few parts in my code, where I would just want to call translate.instant(key) without making a long .subscribe(data =>... part.

Could that kind of function be also implemented in ng2-translate?

Thank you for your work!

👍

@ocombe
Copy link
Member

ocombe commented Jan 27, 2016

Yes, I can code that, but you have to be aware that if your translations haven't been loaded then "instant" would return the key (or undefined).

@lordgreg
Copy link
Author

I am aware of that... that's why I'm trying to load my translation config part in @app constructor, and the translate.instant would I the call in @page controllers/constructor/functions.

But... wouldn't something like this be enough to make this work?

  getInstant(key) {
    return this.translate.get(key).subscribe(data => {return data});
  }

@mebibou
Copy link

mebibou commented Feb 22, 2016

👍 for this, I'm using ng2-translate in an Ionic app and often I need to get a translation for some services that expect to have a string and not an observable.

@mebibou
Copy link

mebibou commented Feb 22, 2016

FYI, I created my own class to do this:

import {Injectable} from 'angular2/core';
import {Parser, TranslateService} from 'ng2-translate/ng2-translate';

@Injectable()
export class TranslationService {
  private parser: Parser = new Parser();

  constructor(public translateService: TranslateService) {}

  public get(key: string|Array<string>, interpolateParams?: Object): string {
    if (!key) {
      throw new Error('Parameter "key" required');
    }

    var getParsedResult = (translations: any, key: any) => {
      var res: string;

      if (key instanceof Array) {
        let result: any = {};
        for (var k of key) {
            result[k] = getParsedResult(translations, k);
        }
        return result;
      }

      if (translations) {
        res = this.parser.interpolate(translations[key], interpolateParams);
      }

      if (typeof res === 'undefined' && this.translateService.defaultLang && this.translateService.defaultLang !== this.translateService.currentLang) {
        let translations: any = this.parser.flattenObject(this.translateService.translations[this.translateService.defaultLang]);
        res = this.parser.interpolate(translations[key], interpolateParams);
      }

      if (!res && this.translateService.missingTranslationHandler) {
        this.translateService.missingTranslationHandler.handle(key);
      }

      return res || key;
    };

    let translations: any;

    if (this.translateService.translations[this.translateService.currentLang]) {
      translations = this.parser.flattenObject(this.translateService.translations[this.translateService.currentLang]);
    }

    return getParsedResult(translations, key);
  }
}

@ocombe
Copy link
Member

ocombe commented Feb 22, 2016

Nice thanks!

@ocombe
Copy link
Member

ocombe commented Feb 28, 2016

This has been added in 1.8.0 !

@mebibou
Copy link

mebibou commented Feb 29, 2016

awesome thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants