Spring MVC - jquery-i18n-properties无法加载消息属性

时间:2015-03-21 11:34:21

标签: jquery angularjs spring spring-mvc

我正在尝试使用AngularJs创建一个Spring MVC 4应用程序。我的问题是AngularJs不支持国际化。我想使用jquery.i18n-properties,但我无法加载messages_%s.properties。

我收到此错误消息: 获取http://localhost:8081/base/resources/i18n/messages.properties?_=1426936125703 404(未找到) jquery.js:7845获取http://localhost:8081/base/resources/i18n/messages_en.properties?_=1426936125704 404(未找到)

应用上下文-config.xml中:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/WEB-INF/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

<mvc:view-controller path="/" view-name="/resources/html/templates/index.html"/>

<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
    id="messageSource" p:defaultEncoding="UTF-8"
    p:basenames="resources/i18n/messages"
    p:fallbackToSystemLocale="false" />

的web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/security-context-config.xml
        /WEB-INF/spring/persistence-context-config.xml
        /WEB-INF/spring/application-context-config.xml
    </param-value>
</context-param>
<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

以下是实际加载文件的内容:

app.service('i18n', function () {
var self = this;
this.setLanguage = function (language) {
    $.i18n.properties({
        name: 'messages',
        path: 'resources/i18n/',
        mode: 'map',
        language: language,
        callback: function () {
            self.language = language;
        }
    });
};
this.setLanguage('en');

});

我不使用JSP-s,我只使用HTML-s。这是我的文件结构:

  src
  |_main
    |_webapp
      |_WEB-INF
        |_resources
          |_...
          |_html
            |_... here are my .html files
          |_i18n
            |_messages_en.proerties
          |_images
            |_...
          |_js
             |_...

我尝试了很多东西来解决这个问题,但没有任何效果。有人可以帮帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

如果您使用AngularJS,我认为应用i18n的最佳方法是使用此lib:https://github.com/angular-translate/angular-translate
这与i18n有很多关系。

答案 1 :(得分:0)

来自homepage的示例:

以下面的Messages.properties,Messages_pt.properties和Messages_pt_PT.properties为例:

<强> Messages.properties

# This line is ignored by the plugin
msg_hello = Hello
msg_world = World
msg_complex = Good morning {0}!

<强> Messages_pt.properties

# We only provide a translation for the 'msg_hello' key
msg_hello = Bom dia

<强> Messages_pt_PT.properties

# We only provide a translation for the 'msg_hello' key
msg_hello = Olá

因此,正如您所看到的,此插件首先加载三个文件:默认文件(例如, Messages.properties ),然后加载特定于语言环境的文件(消息 _pt .properties,然后是消息 _pt_PT .properties)。