Forum Stats

  • 3,769,910 Users
  • 2,253,029 Discussions
  • 7,875,236 Comments

Discussions

Async oj.Translations.setBundle

Nathan Zentner
Nathan Zentner Member Posts: 21 Green Ribbon

I'm wondering what would be the best way to asynchronously load a translation bundle.


I can call "setBundle" on oj.Translations object, but want to make sure that my call to server to get list is complete before I make calls to "getTranslatedString", like I am within appController for menu titles.


How does the functionality decide and also get the locale specific replacements. I'm assuming that if I go down the "setBundle" route, that I would have to load the primary and then the locale specific for any replacements based the the browser locale.


I'm assuming that the locale specific files are just an overloaded file and it would always use the primary translations with the locale specific replacements.

Best Answer

  • Mstarets-Oracle
    Mstarets-Oracle Member Posts: 19 Employee
    Accepted Answer

    Nathan,

    As I said before, there is absolutely no need to call .setBundle() unless you are trying to merge your translations with JET's framework translations. Your code should be able to deal with its bundle independently of JET.

    Regarding the need to wait for the REST call to complete before the app controller logic executes: you are correct that you can wait on a Promise and call .applicationBootstrapComplete() and start your application (usually by calling ko.applyBindings()) when the promise is resolved.


    Max

Answers

  • Mstarets-Oracle
    Mstarets-Oracle Member Posts: 19 Employee

    Hey Nathan,

    I think we should talk about the overall use case first. I suspect that you do not need to use .setBundle() at all. Are you trying to load your own bundle for getting translations in some of your code or are you trying to replace JET's framework bundle? Note that you do not need to use getTranslatedString() for the former case. You can use the static .applyParameters() method (https://jet.oraclecorp.com/jsdocs/oj.Translations.html#applyParameters) to apply parameters to a resource string from your own bundle.

    Also, are you using Require.js for a module loader?


    Max

  • Nathan Zentner
    Nathan Zentner Member Posts: 21 Green Ribbon

    I'm looking for an alternative to hardcoding a bundle. I have a REST API that delivers text key/values now and want to see if there is an easy way to incorporate that within the OOTB resources/nls framework.


    I could have the "config" for ojL10n point to my REST API end point, but I feel like it will work like the rest of RequireJS and append a JS suffix at the end. If I could prevent that, somehow, then that would suffice, because my REST API is returning key/values in the proper way. The only other thing I would need to know is how the subsequent calls would be made for locale specific translations. I'm assuming that if I map to "'myrestapi/nls/l10'" and the client is looking for Spanish, then it would automatically pick "'myrestapi/nls/es/l10'" if defined correctly in the previous call?

  • Mstarets-Oracle
    Mstarets-Oracle Member Posts: 19 Employee

    Hey Nathan,

    Would your REST API be returning a bundle for a certain locale with no need for any further merging? If the answer is "yes", then you may not need Require and ojL10n plugin at all. Couldn't you just parse the json you get from the REST and use it directly as the bundle?


    Max

  • Nathan Zentner
    Nathan Zentner Member Posts: 21 Green Ribbon

    Yes, I am getting JSON from the REST API and I can call "oj.Translations.setBundle" as previously stated. The only issue that I have with this is the timings of it all. I want to get the bundle before my "appController" runs, so that I can use the naming for my menus. I need a way to Asynchronously load it or just make sure my app does not run until my JSON is returned.


    Maybe I could do a promise call within the appController and then within the finally call the "Context.getPageContext().getBusyContext().applicationBootstrapComplete()"?


    mypromise.loadBundle.then(function(myjsonbundle){

    oj.Translations.setBundle(myjsonbundle);

    }).finally(function(({

    Context.getPageContext().getBusyContext().applicationBootstrapComplete();

    });

  • Mstarets-Oracle
    Mstarets-Oracle Member Posts: 19 Employee
    Accepted Answer

    Nathan,

    As I said before, there is absolutely no need to call .setBundle() unless you are trying to merge your translations with JET's framework translations. Your code should be able to deal with its bundle independently of JET.

    Regarding the need to wait for the REST call to complete before the app controller logic executes: you are correct that you can wait on a Promise and call .applicationBootstrapComplete() and start your application (usually by calling ko.applyBindings()) when the promise is resolved.


    Max