Closure Tools

Bidirectional Text Support

Table of Contents

  1. Overview
  2. Bidi Functions in Closure Templates
  3. Bidi Directives in Closure Templates

Overview

Closure Templates support bidirectional text (bidi) with seven functions and two print directives. Note that you might suffer a slight performance loss when using some of these functions or directives because some of them can only be applied at render time.

By default, the template compiler determines a global directionality (LTR or RTL) for the template content based on the language of the message bundle. Provide the message bundle at compile time for the JavaScript source backend and at render time for the Java object backend. If you don't provide a message bundle, the global directionality defaults to LTR. The global directionality only affects the operation of Closure Templates when you explicitly use bidi functions or directives.

To see an example of a template that uses Bidi functions, see the file examples/features.soy in the Closure Templates Subversion repository.

Bidi Functions in Closure Templates

Function Usage
bidiGlobalDir() Provides a way to check the current global directionality. Returns 1 for LTR or -1 for RTL.
bidiDirAttr(text, opt_isHtml)

If the overall directionality of text is different from the global directionality, then this function generates the attribute dir=ltr or dir=rtl, which you can include in the HTML tag surrounding that piece of text. If the overall directionality of text is the same as the global directionality, this function returns the empty string. Set the optional second parameter to true if text contains or can contain HTML tags or HTML escape sequences (default false).

bidiMark() Generates the bidi mark formatting character (LRM or RLM) that corresponds to the global directionality. Note that if you don't want to insert this mark unconditionally, you should use bidiMarkAfter(text) instead.
bidiMarkAfter(text, opt_isHtml)

If the exit (not overall) directionality of text is different from the global directionality, then this function generates either the LRM or RLM character that corresponds to the global directionality. If the exit directionality of text is the same as the global directionality, this function returns the empty string. Set the optional second parameter to true if text contains or can contain HTML tags or HTML escape sequences (default false). You should use this function for an inline section of text that might be opposite directionality from the global directionality. Also, set text to the text that precedes this function.

bidiStartEdge() Generates the string "left" or the string "right", if the global directionality is LTR or RTL, respectively.
bidiEndEdge() Generates the string "right" or the string "left", if the global directionality is LTR or RTL, respectively.
bidiTextDir(text, opt_isHtml) Checks the provided text for its overall (i.e. dominant) directionality. Returns 1 for LTR, -1 for RTL, or 0 for neutral (neither LTR nor RTL). Set the optional second parameter to true if text contains or can contain HTML tags or HTML "escapes" (default false).

Bidi Directives in Closure Templates

Directive Usage
|bidiSpanWrap

If the overall directionality of the print command is different from the global directionality, then the compiler wraps the print command output in a span with dir=ltr or dir=rtl.

Note: The template compiler applies autoescaping before evaluating |bidiSpanWrap, which is safe because |bidiSpanWrap correctly handles HTML-escaped text. If you're manually escaping the output using |escapeHtml, be sure to put |escapeHtml before |bidiSpanWrap, or else you'll end up escaping any span tags that are generated.)

|bidiUnicodeWrap

If the overall directionality the print command is different from the global directionality, then the compiler wraps the print command output with Unicode bidi formatting characters LRE or RLE at the start and PDF at the end.

Note: This directive serves the same purpose as |bidiSpanWrap, but you should only use it in situations where HTML markup is not applicable, for example inside an HTML <option> element.


Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.