Struts 2本地化似乎默认为’en’,如何更改?

huangapple go评论55阅读模式
英文:

Struts 2 locale seems to default to 'en', how to change?

问题

如果我的Struts 2网站设计支持两种语言,并且用户使用第三种(不支持的)语言访问网站,网站如何确定要使用哪种本地化?

如果我的项目列出了以下文件:

  • src/main/resources/i18n/messages.properties # 包含丹麦语字符串
  • src/main/resources/i18n/messages_en.properties # 包含英语字符串

那么我会期望Struts将页面提供给使用德语浏览器请求的用户,但实际情况并非如此!事实上,页面会使用英语本地化中的字符串。为什么会这样,我该如何修改?

要明确一点,如果用户请求 http://localhost:8080/project/index.do?request_locale=de ,那显然不会自动提供德语翻译。尽管如此,如果他们使用德语操作系统和浏览器,那么HTTP标头中将请求该语言。鉴于网站无法满足该请求,Struts如何选择要提供的语言?毕竟它不会返回原始的i18n键,而是从某个本地化返回字符串值。是哪个本地化,为什么?


如何为Struts 2网站设置“备用语言”?它似乎更喜欢“en”,这不是我想要的。

我的项目有以下文件:

  • src/main/webapp/jsp/index.jsp
  • src/main/resources/i18n/messages_da.properties # 包含丹麦语字符串
  • src/main/resources/i18n/messages_en.properties # 包含英语字符串
  • src/main/resources/struts.xml # 包括完整的xml文件,包括:
    • <constant name="struts.custom.i18n.resources" value="i18n.messages" />

加载索引页面时,浏览器中用户设置的首选语言会正确地反映在呈现的页面中。页面上有链接,可以重新加载特定区域设置的页面(http://localhost:8080/project/index.do?request_locale=en),并且这些链接也按预期工作。

然而,如果以不受支持的语言(通过浏览器设置或明确通过 http://localhost:8080/project/index.do?request_locale=sw)访问页面,那么索引页面将以英语呈现。我更希望它以丹麦语显示。如何设置这个备用语言?

甚至将文件更改为:

  • src/main/resources/i18n/messages.properties
  • src/main/resources/i18n/messages_en.properties

似乎没有效果。具体来说,将我的浏览器设置为仅使用斯瓦希里语(以至于英语甚至没有列出),然后在没有特定参数的情况下访问索引页面,网页仍然以英语呈现,而不是使用“裸”i18n文件中的字符串。

我希望在 src/main/resources/struts.xmlsrc/main/webapp/WEB-INF/web.xml 中有一个设置我可能遗漏的设置(因为这是我第一次尝试Struts)。

英文:

Edit to clarify:

If my Struts 2 site is designed to support two languages, and a user comes to the site with a third (unsupported) language, how does the site determine which localisation to use?

If my project lists

  • src/main/resources/i18n/messages.properties # Contains strings in Danish
  • src/main/resources/i18n/messages_en.properties # Contains strings in English

then I would expect Struts to serve the page in Danish to a user whose browser requests it in, say, German. But this is not the case! In reality, the page is served using the strings from the English localisation. Why is that, and how do I modify this?

To be clear, yes if the user requests http://localhost:8080/project/index.do?request_locale=de then that obviously does not magically provide a German translation. All the same, if they use a German OS and browser, then that will be the language requested in the HTTP header. Given that the site is not able to fulfil that request, how does Struts choose which language to serve? It does not, after all, return the raw i18n keys themselves but the string values from some localitation. Which one, and why?


How do I set a 'fallback locale' for a Struts 2 site? It seems to prefer 'en' which is not what I want.

My project has the following files:

  • src/main/webapp/jsp/index.jsp
  • src/main/resources/i18n/messages_da.properties # Contains strings in Danish
  • src/main/resources/i18n/messages_en.properties # Contains strings in English
  • src/main/resources/struts.xml # Complete xml file, including:
    • &lt;constant name=&quot;struts.custom.i18n.resources&quot; value=&quot;i18n.messages&quot; /&gt;

Loading the index page, the preferred language that the user has set in the browser is correctly reflected in the rendered page. The page has links to reload in specific locales (http://localhost:8080/project/index.do?request_locale=en) and these also work as expected.

However, if coming to the page with an unsupported locale (by way of one's browser settings, or explicitly via http://localhost:8080/project/index.do?request_locale=sw) then the index page is rendered in English. I would prefer this to be in Danish. How do I set this fallback language?

Even changing the files to:

  • src/main/resources/i18n/messages.properties
  • src/main/resources/i18n/messages_en.properties

seems to have no effect. Specifically, setting my browser up to use Swahili only (so English is not even listed), and accessing the index page without specific parameters, the web page is still rendered in English and not using the strings from the 'bare' i18n file.

I'm hoping there is a setting in src/main/resources/struts.xml or src/main/webapp/WEB-INF/web.xml that I have missed (as this is my first foray into Struts)?

答案1

得分: 0

以下是已翻译的内容:

我提出了以下解决方案使用自定义 `Interceptor`。我更喜欢在配置文件中进行简单的设置但至少这个方法有效

package my.web.site.i18n;

import java.util.Locale;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LocaleInterceptor implements Interceptor {

    private static final long serialVersionUID = 1L;
    private static final Locale LOCALE_DA = new Locale("da", "DK");
    private static final Locale LOCALE_EN = new Locale("en", "GB");

    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    /**
     * 当用户以非本地化的语言请求我们的网站时,我们希望以丹麦语回应。
     *
     * @param invocation 动作调用
     * @return 传入的调用,可能带有调整后的区域设置
     * @throws Exception
     */
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        String invocationLanguage = invocation.getInvocationContext().getLocale().getLanguage();
        if (!LOCALE_DA.getLanguage().equals(invocationLanguage)
        &&  !LOCALE_EN.getLanguage().equals(invocationLanguage)
        ) {
            invocation.getInvocationContext().setLocale(LOCALE_DA);
        }
        return invocation.invoke();
    }
}
英文:

I came up with the following solution, using a custom Interceptor. I would have preferred a simple entry in a configuration file, but this at least works.

package my.web.site.i18n;

import java.util.Locale;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LocaleInterceptor implements Interceptor {

    private static final long serialVersionUID = 1L;
    private static final Locale LOCALE_DA = new Locale(&quot;da&quot;, &quot;DK&quot;);
    private static final Locale LOCALE_EN = new Locale(&quot;en&quot;, &quot;GB&quot;);

    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    /**
     * When a user requests our site in an un-localised language, we want to respond in Danish.
     *
     * @param invocation the action invocation
     * @return the incoming invocation, possibly with adjusted locale
     * @throws Exception
     */
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        String invocationLanguage = invocation.getInvocationContext().getLocale().getLanguage();
        if (!LOCALE_DA.getLanguage().equals(invocationLanguage)
        &amp;&amp;  !LOCALE_EN.getLanguage().equals(invocationLanguage)
        ) {
            invocation.getInvocationContext().setLocale(LOCALE_DA);
        }
        return invocation.invoke();
    }
}

huangapple
  • 本文由 发表于 2023年6月1日 18:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定