使用当前站点的会话语言进行Orbeon表单本地化 [英] Orbeon form localization using current site's session language

查看:90
本文介绍了使用当前站点的会话语言进行Orbeon表单本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在SAP Commerce(Hybris)中使用ORBEON 2018.2.3.201905172253 PE.从Orbeon文档和我运行的测试中,我了解了表单本地化是通过每个表单中的语言下拉选择器来实现的.问题是,像往常一样,我已经为整个网站设置了语言选择器.

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.

我想到的想法是在加载表单时通过javascript设置表单语言,并使用ajax调用获取当前会话语言.到目前为止,尝试了3种方法,但没有一种起作用:

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. 使用切换语言功能

<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

但是,由于那是2014年的承诺,所以我想有一种实现这一目标的新方法.当该代码运行时,我收到一个JS错误,表示FormRunner是未定义的对象.

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. 设置选择器控件的值:

<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>

但是它也不起作用.

  1. 设置DOM属性(有效),然后调度语言更新事件(不起作用):

<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. 从Orbeon文档中,我可以通过以下方式设置语言: 图片
  1. From Orbeon docs, I can set the language through this: pic

但是,再次,如何在表单初始化期间设置fr语言请求参数或会话属性的值?

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.

预先感谢, 大卫

推荐答案

您可以扩展de.hybris.platform.xyformsservices.proxy.impl.DefaultProxyService rewriteURL方法并通过Orbeon用户语言,如下所示:

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;

希望有帮助

这篇关于使用当前站点的会话语言进行Orbeon表单本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆