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

Empty string renders as missing translation #319

Closed
ruisilva450 opened this issue Nov 7, 2020 · 5 comments
Closed

Empty string renders as missing translation #319

ruisilva450 opened this issue Nov 7, 2020 · 5 comments

Comments

@ruisilva450
Copy link

Describe the bug
I have a typical json being loaded by HttpTranslationLoader in which a token leads to an empty string on that json

To Reproduce
Steps to reproduce the behavior:

  1. Have a return json that looks like this:
{
    'textToTranslate': ''
}
  1. Have a view that tries to translate 'textToTranslate'

Expected behavior
See `` (basically show nothing) instead of textToTranslate

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome
  • Version 10.1.1

Additional context
You can reproduce this on the live demo.
Here is the live demo with changeLocale value set as ""

@ruisilva450
Copy link
Author

As a workaround I did this:

@Injectable()
export class L10nCustomMissingTranslationHandler implements L10nMissingTranslationHandler {
  private translation: L10nTranslationService;

  constructor(@Optional() private injector: Injector) {}

  public handle(key: string): string | any {
    this.translation = this.injector.get(L10nTranslationService);

    const data = this.translation.data;

    let value = null;
    try {
      value = key.split('.').reduce((a, v) => a[v], data[this.translation.getLocale().language]);
    } catch {}

    if (value != null) {
      return value;
    }
    return key;
  }
}

@robisim74
Copy link
Owner

Hi @ruisilva450,

In my opinion, an empty string is a missing translation.

But because this need has already been reported in the past, I will add value parameter to handle method, so everyone can handle it.

@ruisilva450
Copy link
Author

For some locales you can have a use case where you don't want to show certain information and the way some people do it is by putting an empty string.

Therefore my case

@robisim74
Copy link
Owner

I released a new version (10.1.2). Now you should be able to do this:

@Injectable() export class AppMissingTranslationHandler implements L10nMissingTranslationHandler {

    public handle(key: string, value?: string): string | any {
        if (value !== null) return value;
        return key;
    }

}

Let me know.

@ruisilva450
Copy link
Author

Perfect.

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