title: Orbeon form localization using current site's session language
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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:</p>
  
  <ol>
  <li>Using togglelanguage function</li>
  </ol>
  
  <pre><code>&lt;xf:action id="set-language"&gt;
                  &lt;xf:action event="xforms-ready" ev:observer="fr-form-model"
                             if="true()"
                             type="javascript"&gt;
  
                      var languageCode = &lt;AJAX_CALL_TO_MY_SERVER&gt;;
                      ORBEON.FormRunner.toggleLanguage(languageCode);
                  &lt;/xf:action&gt;
              &lt;/xf:action&gt;
  </code></pre>
  
  <p>I got the togglelanguage function from here:</p>
  
  <p><a href="https://github.com/orbeon/orbeon-forms/issues/1559" rel="nofollow noreferrer">https://github.com/orbeon/orbeon-forms/issues/1559</a></p>
  
  <p>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. </p>
  
  <ol start="2">
  <li>Setting the value of the selector control:</li>
  </ol>
  
  <pre><code>&lt;xf:action id="say-hi"&gt;
                  &lt;xf:action event="fr-run-form-load-action-after-controls" ev:observer="fr-form-model"
                             if="true()"
                             type="javascript"&gt;
  
                      ORBEON.xforms.Document.setValue("fr-language-selector", "fr");
                  &lt;/xf:action&gt;
              &lt;/xf:action&gt;
  </code></pre>
  
  <p>But it didn't work either. </p>
  
  <ol start="3">
  <li>Setting the DOM attribute (that worked) and then dispatching a language-update event (no effects):</li>
  </ol>
  
  <pre><code>&lt;xf:action id="say-hi"&gt;
                  &lt;xf:action event="fr-run-form-load-action-after-controls" ev:observer="fr-form-model"
                             if="true()"
                             type="javascript"&gt;
                      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'
                          }
                      );           
                  &lt;/xf:action&gt;
              &lt;/xf:action&gt;
  </code></pre>
  
  <ol start="4">
  <li>From Orbeon docs, I can set the language through this:
  <a href="https://i.stack.imgur.com/M5rWi.png" rel="nofollow noreferrer">pic</a></li>
  </ol>
  
  <p>But again, how can I set the value of fr-language request param or session attribute during form initialization? </p>
  
  <p>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. </p>
  
  <p>Thanks in advance,
  David</p>