javascriptlocalizationsap-commerce-cloudorbeonxforms

Orbeon form localization using current site's session language


I'm using ORBEON 2018.2.3.201905172253 PE within SAP Commerce (Hybris). From Orbeon documentation and the tests I've run, I understand forms localization works through a language dropdown selector within each form. The thing is, I already have a language selector for my entire website, as usual.

So, let's say a user gets into my site, which default language is English. Afterwards, he changes the site's language to Chinese. He is browsing my site in Chinese and navigates to a form which default language is English. He'd need to use the form language selector Again to change it to Chinese! Which from a UX perspective I think we all agree is not a good idea.

The idea I came up with is setting up the language of the form through javascript while the form is loading, getting the current session language using an ajax call. Tried 3 approaches so far, none of them worked:

  1. Using togglelanguage function
<xf:action id="set-language">
                <xf:action event="xforms-ready" ev:observer="fr-form-model"
                           if="true()"
                           type="javascript">

                    var languageCode = <AJAX_CALL_TO_MY_SERVER>;
                    ORBEON.FormRunner.toggleLanguage(languageCode);
                </xf:action>
            </xf:action>

I got the togglelanguage function from here:

https://github.com/orbeon/orbeon-forms/issues/1559

But since that's a commit from 2014 I guess there's a new way of achieving this. When that code runs I get a JS error saying FormRunner is an undefined object.

  1. Setting the value of the selector control:
<xf:action id="say-hi">
                <xf:action event="fr-run-form-load-action-after-controls" ev:observer="fr-form-model"
                           if="true()"
                           type="javascript">

                    ORBEON.xforms.Document.setValue("fr-language-selector", "fr");
                </xf:action>
            </xf:action>

But it didn't work either.

  1. Setting the DOM attribute (that worked) and then dispatching a language-update event (no effects):
<xf:action id="say-hi">
                <xf:action event="fr-run-form-load-action-after-controls" ev:observer="fr-form-model"
                           if="true()"
                           type="javascript">
                    console.log('language:' + ORBEON.util.Dom.getAttribute(document.documentElement, "lang")); 
                    ORBEON.util.Dom.setAttribute(document.documentElement, "lang", "fr");           
                    console.log('language2:' + ORBEON.util.Dom.getAttribute(document.documentElement, "lang")); 
                    ORBEON.xforms.Document.dispatchEvent(
                        {
                            targetId:  'fr-resources-model',
                            eventName: 'fr-update-language'
                        }
                    );           
                </xf:action>
            </xf:action>
  1. From Orbeon docs, I can set the language through this: pic

But again, how can I set the value of fr-language request param or session attribute during form initialization?

I'd like to know if I'm in the correct path for what I want to achieve, and if so, how can I toggle the language of the form while/before loading it.

Thanks in advance, David


Solution

  • You can extend de.hybris.platform.xyformsservices.proxy.impl.DefaultProxyService rewriteURL method and pass Orbeon the user language, something like this:

    URIBuilder builder = null;
            try
            {
                builder = new URIBuilder(this.orbeonAddress + path);
            }
            catch (final URISyntaxException e)
            {
                LOG.error("Error occurred while building the URL : " + e);
                throw new MalformedURLException(e.getMessage());
            }
    
            // if the original URl had a QueryString...
            builder.setQuery(u.getQuery());
    
            // If the form should be embeddable
            if (embeddable)
            {
                builder.addParameter("orbeon-embeddable", "true");
                builder.addParameter("fr-language", getCommonI18NService().getCurrentLanguage().getIsocode());
            }
    
            final String newURL = builder.toString();
    
            LOG.debug("Rewritten URL [" + newURL + "]");
            return newURL;
    
    

    Hope it helps