简体   繁体   English

Freemarker 模板货币符号编码问题

[英]Freemarker template currency symbol encoding issue

Using freemarker template with java is producing an incorrect currency symbol.使用带有 java 的 freemarker 模板会产生不正确的货币符号。 Expecting $ however getting ¤期待$但得到¤

I've tried setting the encoding and outputEncoding on the freemarkerConfig with no luck我试过在 freemarkerConfig 上设置encodingoutputEncoding没有运气

    public String processTemplate(String freemarkerTemplate, Object model, Locale locale) throws IOException, TemplateException {
        try {
            this.freemarkerConfig.setSetting("locale", locale.getLanguage());
            this.freemarkerConfig.setSetting("time_zone", "EST");
        } catch (TemplateException e) {
            log.warn("Failed to set locale {} for freemarker template.", locale.getLanguage(), e);
        }
        //freemarkerConfig.setEncoding(locale, "UTF-8");
        //freemarkerConfig.setOutputEncoding("UTF-8");
        Template template = freemarkerConfig.getTemplate(freemarkerTemplate);
        //template.setOutputEncoding("UTF-8");
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    }

Also have tried adding the <#ftl encoding="utf-8"> to my template file without any difference.还尝试将<#ftl encoding="utf-8">添加到我的模板文件中,没有任何区别。

EDIT编辑

This is how I'm using the currency function: ${amountValue?string.currency}这就是我使用货币 function: ${amountValue?string.currency}的方式

This is not an encoding issue.这不是编码问题。 The ¤ character is called the currency sign, and Java number format prints it if it doesn't know the actual currency. ¤ 字符称为货币符号,如果不知道实际货币,则 Java 数字格式将其打印出来。 The reason it doesn't know it is that you set the FreeMarker locale setting to locale.getLanguage() (like "en"), instead of locale.toString() (like "en_US", which also contains a country), and the language is not enough to decide the currency, it's the country that decides that.它不知道的原因是您将 FreeMarker locale设置设置为locale.getLanguage() (如“en”),而不是locale.toString() (如“en_US”,其中也包含一个国家/地区),并且语言不足以决定货币,是国家决定的。

Also, if you can, avoid setSetting , and call the strongly typed methods, like freemarkerConfig.setLocale(locale) , freemarkerConfig.setTimeZone(timeZone) , etc.此外,如果可以,请避免setSetting并调用强类型方法,如freemarkerConfig.setLocale(locale)freemarkerConfig.setTimeZone(timeZone)等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM