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

Provide an instance of TranslateService to MissingTranslationHandler #221

Closed
DethAriel opened this issue Sep 2, 2016 · 2 comments
Closed

Comments

@DethAriel
Copy link
Contributor

DethAriel commented Sep 2, 2016

I'm submitting a ... (check one with "x")

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[x] feature request

Current behavior
If I implement the MissingTranslationHandler like this:

@Injectable()
export class MyHandler implements MissingTranslationHandler {
  constructor(private translateService: TranslateService) {
  }

  public handle(key: string) {
    return this.translateService.get('MISSING', { original: key });
  }
}

then this results in a cyclic dependency: TranslateService depends on MissingTranslationHandler, which in turn depends on TranslateService.

Expected/desired behavior
The desired behavior would be if the handle method provided an instance of TranslateService, e.g.

public handle(key: string, translateService: TranslateService) {
  return translateService.get('MISSING', { original: key });
}

or, given the #214 proposal, something like

interface TranslateHandleArgs {
  key: string;
  translation: TranslateService;
  interpolateParams?: any;
}

public handle(params: TranslateHandleArgs) {
  return params.translation.get('MISSING', { original: key });
}

Such a thing, in turn, would potentially trigger an infinite loop if the 'MISSING' key is missing, too. This should be taken into account somehow.

What is the motivation / use case for changing the behavior?

Suppose you need to convert API error codes into localized messages. APIs change frequently, and if you haven't yet covered a specific error code, then you need to output something generic along the lines of "An error occurred, code: {{code}}". That is the case this feature request would cover.

Please tell us about your environment:

  • ng2-translate version: 2.4.3
  • Angular version: 2.0.0-rc.6
  • Browser: all
  • Language: TypeScript 1.8.10
@DethAriel
Copy link
Contributor Author

Here's a workaround if someone needs it:

import { Injectable, Injector } from '@angular/core';
import { MissingTranslationHandler, TranslateService } from 'ng2-translate';

@Injectable()
export class CustomMissingTranslationHandler extends MissingTranslationHandler {
  private missingKey: string;
  private translation: TranslateService;
  constructor(private injector: Injector) {
    super();
  }
  public handle(key: string) {
    if (this.translation == null) {
      this.translation = this.injector.get(TranslateService);
    }

    if (this.missingKey != null) {
      // already handling the missing translation, and the '__TRANSLATION_MISSING' part is missing, too.
      // Return the key itself
      let previouslyMissingKey = this.missingKey;
      this.missingKey = null;
      return previouslyMissingKey;
    }

    this.missingKey = key;
    // do your thing using this.translation:
    let result = this.translation.instant('__TRANSLATION_MISSING', { key });
    this.missingKey = null;
    return result;
  }
}

@ocombe
Copy link
Member

ocombe commented Sep 7, 2016

The missing translations handler needs more love, I would love a pull request improving it because I don't have much time to work on this

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

2 participants