Skip to content

Commit 365e8c8

Browse files
committed
Fix #488 add utf8 support to i18n helper
1 parent b5e1fa0 commit 365e8c8

5 files changed

Lines changed: 101 additions & 5 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pagination_top_of_page = Inicio de la página
1+
pagination_top_of_page = Inicio de la página

handlebars/src/main/java/com/github/jknack/handlebars/Handlebars.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package com.github.jknack.handlebars;
1919

20+
import com.github.jknack.handlebars.helper.I18nHelper;
2021
import static org.apache.commons.lang3.Validate.isTrue;
2122
import static org.apache.commons.lang3.Validate.notEmpty;
2223
import static org.apache.commons.lang3.Validate.notNull;
@@ -1369,6 +1370,8 @@ public Handlebars setCharset(final Charset charset) {
13691370
this.charset = notNull(charset, "Charset required.");
13701371
registry.setCharset(charset);
13711372
loader.setCharset(charset);
1373+
I18nHelper.i18n.setCharset(charset);
1374+
I18nHelper.i18nJs.setCharset(charset);
13721375
return this;
13731376
}
13741377

handlebars/src/main/java/com/github/jknack/handlebars/helper/I18nHelper.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
import static org.apache.commons.lang3.Validate.notNull;
2424

2525
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.InputStreamReader;
28+
import java.nio.charset.Charset;
29+
import java.nio.charset.StandardCharsets;
2630
import java.text.MessageFormat;
2731
import java.util.ArrayList;
2832
import java.util.Enumeration;
2933
import java.util.List;
3034
import java.util.Locale;
35+
import java.util.PropertyResourceBundle;
3136
import java.util.ResourceBundle;
3237
import java.util.regex.Matcher;
3338
import java.util.regex.Pattern;
@@ -158,7 +163,7 @@ public Object apply(final String key, final Options options) throws IOException
158163
String baseName = options.hash("bundle", defaultBundle);
159164
ClassLoader classLoader = options.hash("classLoader", getClass().getClassLoader());
160165
I18nSource localSource = source == null
161-
? new DefI18nSource(baseName, locale, classLoader) : source;
166+
? new DefI18nSource(charset, baseName, locale, classLoader) : source;
162167

163168
return localSource.message(key, locale, options.params);
164169
}
@@ -232,7 +237,7 @@ public Object apply(final String localeName, final Options options) throws IOExc
232237
String baseName = options.hash("bundle", defaultBundle);
233238
ClassLoader classLoader = options.hash("classLoader", getClass().getClassLoader());
234239
I18nSource localSource = source == null
235-
? new DefI18nSource(baseName, locale, classLoader) : source;
240+
? new DefI18nSource(charset, baseName, locale, classLoader) : source;
236241
StringBuilder buffer = new StringBuilder();
237242
Boolean wrap = options.hash("wrap", true);
238243
if (wrap) {
@@ -292,6 +297,20 @@ private String message(final String message) {
292297
/** The message source to use. */
293298
protected I18nSource source;
294299

300+
/** Charset. **/
301+
protected Charset charset = StandardCharsets.UTF_8;
302+
303+
/**
304+
* Set the charset to use.
305+
*
306+
* NotThreadSafe Make sure to call this method ONCE at start time.
307+
*
308+
* @param charset Charset. Required.
309+
*/
310+
public void setCharset(final Charset charset) {
311+
this.charset = notNull(charset, "Charset required.");
312+
}
313+
295314
/**
296315
* Set the message source.
297316
*
@@ -332,18 +351,60 @@ public void setDefaultLocale(final Locale locale) {
332351
/** Default implementation of I18nSource. */
333352
class DefI18nSource implements I18nSource {
334353

354+
/**
355+
* UTF8 resource bundle control.
356+
*
357+
* Source: Source: ckoverflow.com/questions/4659929
358+
*
359+
* @author edgar
360+
*
361+
*/
362+
public static class UTF8Control extends ResourceBundle.Control {
363+
/** Charset. */
364+
private final Charset charset;
365+
366+
/**
367+
* Creates a new utf8 control.
368+
*
369+
* @param charset Charset.
370+
*/
371+
public UTF8Control(final Charset charset) {
372+
this.charset = charset;
373+
}
374+
@Override
375+
public ResourceBundle newBundle(final String baseName, final Locale locale, final String format,
376+
final ClassLoader loader, final boolean reload) throws IOException {
377+
// The below is a copy of the default implementation.
378+
String bundleName = toBundleName(baseName, locale);
379+
String resourceName = toResourceName(bundleName, "properties");
380+
InputStream stream = null;
381+
try {
382+
stream = loader.getResourceAsStream(resourceName);
383+
PropertyResourceBundle bundle = new PropertyResourceBundle(
384+
new InputStreamReader(stream, charset));
385+
return bundle;
386+
} finally {
387+
if (stream != null) {
388+
stream.close();
389+
}
390+
}
391+
}
392+
}
393+
335394
/** The resource bundle. */
336395
private ResourceBundle bundle;
337396

338397
/**
339398
* Creates a new {@link DefI18nSource}.
340399
*
400+
* @param charset Charset to use.
341401
* @param baseName The base name.
342402
* @param locale The locale.
343403
* @param classLoader The classloader.
344404
*/
345-
public DefI18nSource(final String baseName, final Locale locale, final ClassLoader classLoader) {
346-
bundle = ResourceBundle.getBundle(baseName, locale, classLoader);
405+
public DefI18nSource(final Charset charset, final String baseName, final Locale locale,
406+
final ClassLoader classLoader) {
407+
bundle = ResourceBundle.getBundle(baseName, locale, classLoader, new UTF8Control(charset));
347408
}
348409

349410
@Override
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.jknack.handlebars;
2+
3+
import com.github.jknack.handlebars.helper.I18nHelper;
4+
import org.junit.After;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
import java.util.Locale;
10+
11+
public class Issue488 extends AbstractTest {
12+
13+
@Before
14+
public void overrideDefaults() {
15+
I18nHelper.i18n.setDefaultBundle("i488");
16+
I18nHelper.i18n.setDefaultLocale(Locale.ENGLISH);
17+
}
18+
19+
@After
20+
public void defaults() {
21+
I18nHelper.i18n.setDefaultBundle("messages");
22+
I18nHelper.i18n.setDefaultLocale(Locale.getDefault());
23+
}
24+
25+
@Test
26+
public void utf8() throws IOException {
27+
shouldCompileTo("{{i18n \"utf8\"}}", $, "Bonjour à tous.");
28+
}
29+
30+
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
utf8=Bonjour à tous.

0 commit comments

Comments
 (0)