首页 > 解决方案 > 是否有任何选项可以在 JSP 中显示其他语言文本?

问题描述

我正在使用 Spring MVC(Spring-Security)项目,当用户从给定的下拉菜单中更改语言时,应该显示特定语言的给定文本。I tried in the following way but I am getting ??????????? in the browser when Hindi or Marathi text is selected.

页面

这是用户可以更改语言的下拉菜单

<div class="dropdown">
    <select id="languageId">
        <option value="English">English</option>
        <option value="Hindi">Hindi</option>
        <option value="Marathi">Marathi</option>
    </select>           
</div>

这是选择特定语言时显示的文本框:

<label id="english">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500.
</label>

<label id="hindi" class="KrutiDev_hindi_text">
खरिदने एकत्रित अपनि लक्षण गटको लक्ष्य मार्गदर्शन पहोच। सहायता और्४५० मुक्त काम संसाध गयेगया प्रौध्योगिकी आपको जैसे बाटते सभिसमज प्रव्रुति कीसे पहोचने वार्तालाप विचरविमर्श विवरण तरीके लचकनहि 
</label>

<label id="marathi">
इनपुट साधने वेबवर कुठेही, आपण निवडलेल्‍या भाषेमध्‍ये टाइप करणे सोपे बनवतात.
</label>

更改下拉选项的 Javascript 代码:

 $(function () {
    $("#languageId").change(function () {
    if($(this).val() == 'Hindi') {
        var hindi = document.getElementById('hindi');
        hindi.style.removeProperty("display");
        $('#english').css('display','none');
        $('#marathi').css('display','none');
    }
    if($(this).val() == 'English') {
        var english = document.getElementById('english');
        english.style.removeProperty("display");
        $('#hindi').css('display','none');
        $('#marathi').css('display','none');
    }
    if($(this).val() == 'Marathi') {
        var marathi = document.getElementById('marathi');
        marathi.style.removeProperty("display");
        $('#hindi').css('display','none');
        $('#english').css('display','none');
    }
});
}); 

CSS

@font-face {
font-family: 'Kruti Dev';
src: url('fonts/Kruti-Dev.ttf') format('truetype')}

.KrutiDev_hindi_text {
font-family: Kruti Dev !important;
font-size: 12px !important;}

我还从属性 -> 资源添加<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">和更新了文本文件编码。UTF_8

任何人都可以帮我解决我所缺少的内容,以便我可以获得其他语言的文本。

标签: javascriptspring-mvcspring-securityutf-8

解决方案


如果有人想显示其他语言或 UTF-8 字符,

当我在配置文件中添加它时它对我有用:

添加消息源

MessageSource 帮助应用程序开发人员通过编写大量额外代码来处理各种复杂场景,例如特定于环境的配置、国际化或可配置值。

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
         <property name="defaultEncoding" value="UTF-8" />
</bean>
    
<bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationMessageSource" ref="messageSource"/>
</bean>
     
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
</bean>
    
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
</bean> 

我添加了messages.properties

hindi=\u092E\u0948\u0902 \u0935\u0947\u0930\u093E\u0938\u093F\u0938 

marathi=\u092E\u0948\u0902 \u0935\u0947\u0930\u093E\u0938\u093F\u0938 

我还更新了我的 jsp:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<spring:message code="hindi"/>

<spring:message code="marathi"/>

推荐阅读