| <!doctype html> |
| <!-- Generated by FreeMarker/Docgen from DocBook --> |
| <html lang="en" class="page-type-section"> |
| <head prefix="og: http://ogp.me/ns#"> |
| <meta charset="utf-8"> |
| <title>Charset issues - FreeMarker Manual</title> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <meta name="format-detection" content="telephone=no"> |
| <meta property="og:site_name" content="FreeMarker Manual"> |
| <meta property="og:title" content="Charset issues"> |
| <meta property="og:locale" content="en_US"> |
| <meta property="og:url" content="http://example.com/pgui_misc_charset.html"> |
| <link rel="canonical" href="http://example.com/pgui_misc_charset.html"> |
| <link rel="icon" href="favicon.png" type="image/png"> |
| <link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338517553"> |
| </head> |
| <body itemscope itemtype="https://schema.org/Code"> |
| <meta itemprop="url" content="http://example.com/"> |
| <meta itemprop="name" content="FreeMarker Manual"> |
| |
| <!--[if lte IE 9]> |
| <div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div> |
| <![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo"> |
| </a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui.html"><span itemprop="name">Programmer's Guide</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui_misc.html"><span itemprop="name">Miscellaneous</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="pgui_misc_charset.html"><span itemprop="name">Charset issues</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li></ul></div></div></div> <div class="main-content site-width"> |
| <div class="content-wrapper no-toc"> |
| <div id="table-of-contents-wrapper" class="col-left"> |
| </div> |
| <div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="pgui_misc_var.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui_misc_multithreading.html"><span>Next</span></a></div><div class="title-wrapper"> |
| <h1 class="content-header header-section1" id="pgui_misc_charset" itemprop="headline">Charset issues</h1> |
| </div></div><div class="page-menu"> |
| <div class="page-menu-title">Page Contents</div> |
| <ul><li><a class="page-menu-link" href="#autoid_46" data-menu-target="autoid_46">The charset of the input</a></li><li><a class="page-menu-link" href="#autoid_47" data-menu-target="autoid_47">The charset of the output</a></li></ul> </div><p>FreeMarker, as most Java applications, works with "<a href="gloss.html#gloss.unicode">UNICODE</a> text" (UTF-16). Nonetheless, |
| there are situations when it must deal with <a href="gloss.html#gloss.charset">charsets</a>, because it has to exchange |
| data with the outer world that may uses various charsets.</p> |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_46">The charset of the input</h2> |
| |
| |
| <p>When FreeMarker has to load a template file (or an unparsed |
| text file), then it must know the charset of the file, since files |
| are just raw byte arrays. You can use the |
| <code class="inline-code">encoding</code> <a href="pgui_config_settings.html">setting</a> to specify the |
| charset. This setting takes effect only when FreeMarker loads a |
| template (parsed or unparsed) with the |
| <code class="inline-code">getTemplate</code> method of |
| <code class="inline-code">Configuration</code>. Note that the <a href="ref_directive_include.html#ref.directive.include"><code>include</code> |
| directive</a> uses this method internally, so the value of the |
| <code class="inline-code">encoding</code> setting is significant for an already |
| loaded template if the template contains <code class="inline-code">include</code> |
| directive call.</p> |
| |
| <p>The getter and setter method of the |
| <code class="inline-code">encoding</code> setting is special in the first |
| (configuration) layer. The getter method guesses the return value |
| based on a <code class="inline-code">Locale</code> passed as parameter; it looks |
| up the encoding in a table that maps locales to encodings (called |
| encoding map), and if the locale was not found there, it returns the |
| default encoding. You can fill the encoding map with the |
| <code class="inline-code">setEncoding(Locale locale, String encoding)</code> |
| method of the configuration; the encoding map is initially empty. |
| The default encoding is initially the value of the |
| <code class="inline-code">file.encoding</code> system property, but you can set a |
| different default with the <code class="inline-code">setDefaultEncoding</code> |
| method.</p> |
| |
| <p>You can give the charset directly by overriding the |
| <code class="inline-code">encoding</code> setting in the template layer or runtime |
| environment layer (When you specify an encoding as the parameter of |
| <code class="inline-code">getTemplate</code> method, you override the |
| <code class="inline-code">encoding</code> setting in the template layer.). If you |
| don't override it, the effective value will be what the |
| <code class="inline-code">configuration.getEncoding(Locale)</code> method returns |
| for the effective value of the <code class="inline-code">locale</code> |
| setting.</p> |
| |
| <p>Also, instead of relying on this charset guessing mechanism, |
| you can specify the charset of the template in the template file |
| itself, with the <a href="ref_directive_ftl.html#ref.directive.ftl"><code>ftl</code></a> |
| directive.</p> |
| |
| <p>You may wonder what charset you should choose for the |
| templates. It primarily depends on the tools (as text editors) that |
| you want to use to create and modify templates. In principle, using |
| UTF-8 is the best, but currently (2004) only a few tools supports |
| it, actually most of them doesn't even know about charsets. So in |
| that case you should use the widely used charset of your language, |
| which is probably also the default charset of you working |
| environment.</p> |
| |
| <p>Note that the charset of the template is independent from the |
| charset of the output that the tempalte generates (unless the |
| enclosing software deliberately sets the output charset to the same |
| as the template charset).</p> |
| |
| |
| |
| |
| |
| <h2 class="content-header header-section2" id="autoid_47">The charset of the output</h2> |
| |
| |
| <div class="callout note"> |
| <strong class="callout-label">Note:</strong> |
| |
| <p>The <code class="inline-code">output_encoding</code> setting/variable and |
| the <a href="ref_builtins_string.html#ref_builtin_url"><code>url</code> |
| built-in</a> is available since FreeMarker 2.3.1. It doesn't |
| exist in 2.3.</p> |
| </div> |
| |
| |
| <p>In principle FreeMarker does not deal with the charset |
| of the output, since it writes the output to a |
| <code class="inline-code">java.io.Writer</code>. Since the |
| <code class="inline-code">Writer</code> is made by the software that encapsulates |
| FreeMarker (such as a Web application framework), the output charset |
| is controlled by the encapsulating software. Still, FreeMarker has a |
| setting called <code class="inline-code">output_encoding</code> (starting from |
| FreeMarker version 2.3.1). The enclosing software should set this |
| setting (to the charset that the <code class="inline-code">Writer</code> uses), to |
| inform FreeMarker what charset is used for the output (otherwise |
| FreeMarker can't find it out). Some features, such as the <a href="ref_builtins_string.html#ref_builtin_url"><code>url</code> built-in</a>, |
| and the <a href="ref_specvar.html"><code>output_encoding</code> special |
| variable</a> utilize this information. Thus, if the enclosing |
| software doesn't set this setting then FreeMarker features that need |
| to know the output charset can't be used.</p> |
| |
| <p>If you write software that will use FreeMarker, you may wonder |
| what output charset should you choose. Of course it depends on the |
| consumer of the FreeMarker output, but if the consumer is flexible |
| regarding this question, then the common practice is either using |
| the charset of the template file for the output, or using UTF-8. |
| Using UTF-8 is usually a better practice, because arbitrary text may |
| comes from the data-model, which then possibly contains characters |
| that couldn't be encoded with the charset of the template.</p> |
| |
| <p>FreeMarker settings can be set for each individual template |
| processing if you use |
| <code class="inline-code">Template.createProcessingEnvironment(<em class="code-color">...</em>)</code> |
| plus |
| <code class="inline-code">Environment.process(<em class="code-color">...</em>)</code> |
| instead of |
| <code class="inline-code">Template.process(<em class="code-color">...</em>)</code>. |
| Thus, you can set the <code class="inline-code">output_encoding</code> setting for |
| each template execution independently:</p> |
| |
| |
| |
| <div class="code-wrapper"><pre class="code-block code-unspecified">Writer w = new OutputStreamWriter(out, outputCharset); |
| Environment env = template.createProcessingEnvironment(dataModel, w); |
| env.setOutputEncoding(outputCharset); |
| env.process();</pre></div> |
| <div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="pgui_misc_var.html"><span>Previous</span></a><a class="paging-arrow next" href="pgui_misc_multithreading.html"><span>Next</span></a></div></div></div></div> </div> |
| </div> |
| <div class="site-footer"><div class="site-width"><div class="footer-bottom"> <p class="last-generated"> |
| Last generated: |
| <time itemprop="dateModified" datetime="2020-07-09T23:48:37Z" title="Thursday, July 9, 2020 11:48:37 PM GMT">2020-07-09 23:48:37 GMT</time> </p> |
| <p class="copyright"> |
| © <span itemprop="copyrightYear">1999</span>–2020 |
| <a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p> |
| </div></div></div></body> |
| </html> |