Code Monkey home page Code Monkey logo

Comments (6)

robisim74 avatar robisim74 commented on May 23, 2024

Hi,

the problem is not the browser, although it's possible that Chrome is so fast to complete the loading of the json file before uploading the application. The problem is you can not call the translate method before the http get request of the json file is completed.
If you want to get a translation into your component while loading the application, I have to change the LocalizationService class to emit an event when the translation changes, so that you can subscribe to the event in this way:

    this.localization.translationChanged.subscribe(

        // Generator or next.
        (value: boolean) => { console.log(this.localization.translate('KEY_FOR_RESOURCE')); }

    );

Tell me if this can be useful, and I'll implement the change.

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024

I added the commit to the LocalizationService class that allows you to subscribe to the event as explained above.
If you want to test it, you can do so:

  • Uninstall this library from your application
  • Download this repository
  • Follow the instructions of the README file to build the library and install it in your application

If it solves your problem, I'll include the commit in the next version of the library.

from angular-l10n.

djb12 avatar djb12 commented on May 23, 2024

Hello robisim74,

Your implementation works wonderfully. Thank you very much.

There are still problems, however, when developping for more than one browser since Chrome is so fast. The translationChanged event does not get triggered for components loaded at application start in Chrome while it does be triggered in the Microsoft browsers.

At the moment, I have to set the translations inline when my variables are initialized for Chrome:

public title = this.localization.translate('TITLE_KEY');

For other browsers, I have to set them again in the subscribe method:

this.localization.translationChanged.subscribe((value: boolean) => this.title = this.localization.translate('TITLE_KEY'));

This is okay, I guess, but it leads a certain amount of code duplication when the translation method needs to be called inside .ts/.js.

Don't know a way to avoid that, however .

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024

Strange, I tested it and it works fine without initialize it. Can you post the code of your component? This evening I'll try it.

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024

Hi @djb12,

I confirm to you that in my test it's never necessary to initialize the values of translations for Chrome.

However, as regards the bootstrap component, you could solve definitively the problem by calling the updateTranslation method (which is responsible for the http request) at the end of the constructor:

@Component({
    selector: 'my-app',
    template: '<h1>{{ title }}</h1>'
})
export class AppComponent extends Locale {

    public title: string;

    constructor(public locale: LocaleService, public localization: LocalizationService) {
        super(null, localization);

        this.locale.addLanguage('en');
        this.locale.definePreferredLanguage('en', 5);
        this.localization.translationProvider('./resources/locale-');

        this.localization.translationChanged.subscribe(

            () => { this.title = this.localization.translate('TITLE'); }

        );

        this.localization.updateTranslation();
    }

}

Please note that in all the other components in an app where you have a router, you should still initialize variables with the translation, because these components are loaded only when they are required, and after the initial event , the new property translationChanged is only updated when the selected language changes.

from angular-l10n.

robisim74 avatar robisim74 commented on May 23, 2024

I released the new version of the library that includes the commit. Here you can also find an example of the document title translation.

from angular-l10n.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.