【问题标题】:spring project not displaying bengali font春季项目不显示孟加拉语字体
【发布时间】:2015-08-13 11:53:58
【问题描述】:

在我的项目中,我想用孟加拉语显示一条消息。所以,我有:

style.css

    @font-face {
      font-family: 'Siyam Rupali';
      src: url('../fonts/SiyamRupali.eot?version=1.070');
      src: local('Siyam Rupali'),
       url('../fonts/SiyamRupali.woff?version=1.070') format('woff'),     
         url('../fonts/SiyamRupali.ttf?version=1.070') format('truetype');
      font-style: normal;

    }

.tabs {
    position: relative;
    margin: 10px auto;
    font-family: 'Siyam Rupali';

}

我在 WEB-INF 文件夹中有“message.properties”。 message.properties 是:

tab1.name = \u09AC\u09BE\u0982\u09B2\u09BE

我有 spring-servlet.xml

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
         <property name="defaultEncoding" value="UTF-8" />

    </bean>

我正在使用 maven,所以,在 Pom.xml 我有:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

在我的 jsp 页面中,我有:

    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<section class="tabs">
    <spring:message code="tab1.name"/>
</ssection>

但是消息显示为question mark..(????).. 为什么??

【问题讨论】:

    标签: java css spring maven spring-mvc


    【解决方案1】:

    有点晚了,但我也遇到了同样的问题,所以我想分享一下我是如何解决这个问题的,希望它对未来的人有所帮助。此外,这不是 Bangla 字体特有的,也适用于任何其他 unicode 字体。

    首先,你的 CSS 看起来不错。这就是我添加字体的方式。这是我包含字体的 .css 文件:

    @font-face {
        font-family: 'Siyam Rupali';
    
        src: url('../fonts/SiyamRupali.eot');
    
        src: local('Siyam Rupali'),
            url('../fonts/SiyamRupali.eot')     format('embedded-opentype'),
            url('../fonts/SiyamRupali.woff')    format('woff'),
            url('../fonts/SiyamRupali.ttf')     format('truetype');
    
        font-style: normal;
    }
    
    body {
        font-family: 'Siyam Rupali', Fallback, sans-serif;
    }
    

    您仍然可以毫无问题地在标签上使用特定的字体系列。

    接下来是您的资源文件。这是 IDE 的一个众所周知的问题,它们通常不会创建具有必要编码的文件(大部分时间是 ASCII)。在记事本中打开您的消息属性文件并将它们保存为 UTF-8。同样从 IDE 的文件编码设置中,确保属性文件是 UTF-8。 See this picture

    接下来是配置,在您的 html(或模板页面)中添加元属性 charset='UTF-8'。

    在您的 spring 配置中,(据我所知,我使用基于 java 的配置和 Thymeleaf 模板引擎,xml 配置和 jsp+jstl 视图解析器配置将是相似的)

    @Configuration
    public class ThymeleafConfig {
    
        @Bean
        public ReloadableResourceBundleMessageSource messageSource() {
            ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
            resource.setBasename("classpath:i18n/messages");
            resource.setDefaultEncoding("UTF-8");
            return resource;
        }
    
        @Bean
        public ServletContextTemplateResolver templateResolver() {
            ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
            resolver.setPrefix("/WEB-INF/html/");
            resolver.setSuffix(".html");
            resolver.setTemplateMode("HTML5");
            resolver.setOrder(1);
            resolver.setCharacterEncoding("UTF-8");
            return resolver;
        }
    
        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine engine = new SpringTemplateEngine();
            engine.setTemplateResolver(templateResolver());
            return engine;
        }
    
        @Bean
        public ThymeleafViewResolver thymeleafViewResolver() {
            ThymeleafViewResolver resolver = new ThymeleafViewResolver();
            resolver.setTemplateEngine(templateEngine());
            resolver.setCharacterEncoding("UTF-8");
            return resolver;
        }
    }
    

    非常重要的是要注意,您的 视图解析器 bean(在我的例子中是 ThymeleafViewResolver)必须将字符编码设置为 UTF-8 并作为消息源,使用ReloadableResourceBundleMessageSource 并将字符编码指定为 UTF-8。或者相信我的话,随时随地设置 UTF-8。

    希望这会有所帮助。

    【讨论】:

    • 如果 nano progga 字体来自this WP theme,那么请注意,这个字体只是一个图标字体,它不包含任何孟加拉语字形。
    • 是的,我后来删除了它,因为我只使用了 Siyam Rupali。我会更新帖子。
    猜你喜欢
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多