Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker encoding - question marks in the place of accented characters

I am trying to print accented characters with Freemarker, but in the place of accented characters, I get only question marks. I have verified, that following statement holds:

 "UTF-8" == Environment.getCurrentEnvironment().getConfiguration().getDefaultEncoding()

I can easily see, that the accented characters are correctly held in the variable before giving it to the template.

My freemarker context can be found here: https://gist.github.com/1975239

For instance instead of:

 Jedinečný živý koncert, kde nejen, že uslyšíte, ale i uvidíte splynutí metalové kapely s padesátičlenným orchestrem včetně.

I keep getting:

 Jedine?ný ?ivý koncert, kde nejen, ?e usly?íte, ale i uvidíte splynutí metalové kapely s padesáti?lenným orchestrem v?etn?.

Thanks.

like image 529
Vojtěch Avatar asked Mar 04 '12 23:03

Vojtěch


People also ask

How do you escape special characters in FreeMarker?

esc creates a markup output value out of a string value by escaping all special characters in it.

What is eval in FreeMarker?

eval. This built-in evaluates a string as an FTL expression. For example "1+2"? eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)

How do you comment on FreeMarker?

Comments: Comments are similar to HTML comments, but they are delimited by <#-- and --> . Comments will be ignored by FreeMarker, and will not be written to the output.

How do I print a value in FreeMarker?

For example, if you create a variable called "foo" in the template, you can print its value with ${foo} . If, coincidently, there's a variable called "foo" in the data-model too, the variable created in the template will hide (not overwrite!)


2 Answers

For DROPWIZARD Users: passing through the UTF-8 Charset in the constructor worked out:

import io.dropwizard.views.View;

import java.nio.charset.Charset;

public class SomeView extends View {
    public SomeView() {
        super("/views/some.ftl", Charset.forName("UTF-8"));
    }
}
like image 181
AndacAydin Avatar answered Oct 18 '22 10:10

AndacAydin


I was able to resolve a similar issue with non-standard symbols (like ™) by setting the content-type on the FreeMarkerViewResolver:

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
...
    <property name="contentType" value="text/html;charset=UTF-8"/>
...
</bean>
like image 31
Stano Avatar answered Oct 18 '22 10:10

Stano