Resolving spring:messages in javascript for i18n internationalization

后端 未结 4 2229
醉梦人生
醉梦人生 2020-12-02 15:57

I\'m attempting to internationalize some of our code. I have a page in JSPX which is using the tag to resolve strings from a messag

4条回答
  •  情书的邮戳
    2020-12-02 16:18

    In addition to answer of @Toilal you can add a helper function to strings.jsp to do a better use of the translation array:

    <%@page contentType="text/javascript" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    var messages = new Array();
    
    messages[""] = "";
    
    
    /**
     * Tranlate a String by key, if key is not defined return the key.
     *  
     * @author Pedro Peláez , Drupal/Wordpress authors, and others
     * @param {String} key
     * @returns {String}
     */
    function t(key) {
        if (messages[key]) {
            return messages[key];
        }
        return key;
    }
    

    Then when needed just use:

    alert(t("menu.section.main"));
    

提交回复
热议问题