i18n.bidi

Classes

goog.i18n.bidi.DirectionalString
Strings that have an (optional) known direction. Implementations of this interface are string-like objects that carry an attached direction, if known.

Public Protected Private

Enumerations

goog.i18n.bidi.Dir :
Directionality enum.
Constants:
LTR
No description.
NEUTRAL
No description.
RTL
No description.
UNKNOWN
No description.
Code »
goog.i18n.bidi.Format :
Unicode formatting characters and directionality string constants.
Constants:
LRE
No description.
LRM
No description.
PDF
No description.
RLE
No description.
RLM
No description.
Code »

Global Functions

goog.i18n.bidi.detectRtlDirectionality(stropt_isHtml) boolean
Check the directionality of a piece of text, return true if the piece of text should be laid out in RTL direction.
Arguments:
str : string
The piece of text that need to be detected.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether this piece of text should be laid out in RTL.
code »
goog.i18n.bidi.endsWithLtr(stropt_isHtml) boolean
Check if the exit directionality a piece of text is LTR, i.e. if the last strongly-directional character in the string is LTR.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether LTR exit directionality was detected.
code »
goog.i18n.bidi.endsWithRtl(stropt_isHtml) boolean
Check if the exit directionality a piece of text is RTL, i.e. if the last strongly-directional character in the string is RTL.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether RTL exit directionality was detected.
code »
goog.i18n.bidi.enforceLtrInHtml(html) string
Enforce the html snippet in RTL directionality regardless overall context. If the html piece was enclosed by tag, dir will be applied to existing tag, otherwise a span tag will be added as wrapper. For this reason, if html snippet start with with tag, this tag must enclose the whole piece. If the tag already has a dir specified, this new one will override existing one in behavior (tested on FF and IE).
Arguments:
html : string
The string that need to be processed.
Returns: string  The processed string, with directionality enforced to RTL.
code »
goog.i18n.bidi.enforceLtrInText(text) string
Enforce LTR on both end of the given text piece using unicode BiDi formatting characters LRE and PDF.
Arguments:
text : string
The piece of text that need to be wrapped.
Returns: string  The wrapped string after process.
code »
goog.i18n.bidi.enforceRtlInHtml(html) string
Enforce the html snippet in RTL directionality regardless overall context. If the html piece was enclosed by tag, dir will be applied to existing tag, otherwise a span tag will be added as wrapper. For this reason, if html snippet start with with tag, this tag must enclose the whole piece. If the tag already has a dir specified, this new one will override existing one in behavior (tested on FF and IE).
Arguments:
html : string
The string that need to be processed.
Returns: string  The processed string, with directionality enforced to RTL.
code »
goog.i18n.bidi.enforceRtlInText(text) string
Enforce RTL on both end of the given text piece using unicode BiDi formatting characters RLE and PDF.
Arguments:
text : string
The piece of text that need to be wrapped.
Returns: string  The wrapped string after process.
code »
goog.i18n.bidi.estimateDirection(stropt_isHtml) goog.i18n.bidi.Dir
Estimates the directionality of a string based on relative word counts. If the number of RTL words is above a certain percentage of the total number of strongly directional words, returns RTL. Otherwise, if any words are strongly or weakly LTR, returns LTR. Otherwise, returns UNKNOWN, which is used to mean "neutral". Numbers are counted as weakly LTR.
Arguments:
str : string
The string to be checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: goog.i18n.bidi.Dir  Estimated overall directionality of str.
code »
goog.i18n.bidi.guardBracketInHtml(sopt_isRtlContext) string
Apply bracket guard using html span tag. This is to address the problem of messy bracket display frequently happens in RTL layout.
Arguments:
s : string
The string that need to be processed.
opt_isRtlContext : boolean=
specifies default direction (usually direction of the UI).
Returns: string  The processed string, with all bracket guarded.
code »
goog.i18n.bidi.guardBracketInText(sopt_isRtlContext) string
Apply bracket guard using LRM and RLM. This is to address the problem of messy bracket display frequently happens in RTL layout. This version works for both plain text and html. But it does not work as good as guardBracketInHtml in some cases.
Arguments:
s : string
The string that need to be processed.
opt_isRtlContext : boolean=
specifies default direction (usually direction of the UI).
Returns: string  The processed string, with all bracket guarded.
code »
goog.i18n.bidi.hasAnyLtr(stropt_isHtml) boolean
Test whether the given string has any LTR characters in it.
Arguments:
str : string
The given string that need to be tested.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether the string contains LTR characters.
code »
goog.i18n.bidi.hasAnyRtl(stropt_isHtml) boolean
Test whether the given string has any RTL characters in it.
Arguments:
str : string
The given string that need to be tested.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether the string contains RTL characters.
code »
goog.i18n.bidi.hasRtlChar(str) boolean
Use hasAnyRtl. Test whether the given string has any RTL characters in it.
Arguments:
str : string
The given string that need to be tested.
Returns: boolean  Whether the string contains RTL characters.
code »
goog.i18n.bidi.isLtrChar(str) boolean
Check if the first character in the string is LTR or not.
Arguments:
str : string
The given string that need to be tested.
Returns: boolean  Whether the first character in str is an LTR char.
code »
goog.i18n.bidi.isLtrExitText(stropt_isHtml) boolean
Use endsWithLtr. Check if the exit directionality a piece of text is LTR, i.e. if the last strongly-directional character in the string is LTR.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether LTR exit directionality was detected.
code »
goog.i18n.bidi.isLtrText(stropt_isHtml) boolean
Use startsWithLtr. Check whether the first strongly directional character (if any) is LTR.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether LTR directionality is detected using the first strongly-directional character method.
code »
goog.i18n.bidi.isNeutralChar(str) boolean
Check if the first character in the string is neutral or not.
Arguments:
str : string
The given string that need to be tested.
Returns: boolean  Whether the first character in str is a neutral char.
code »
goog.i18n.bidi.isNeutralText(stropt_isHtml) boolean
Check whether the input string either contains no strongly directional characters or looks like a url.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether neutral directionality is detected.
code »
goog.i18n.bidi.isRtlChar(str) boolean
Check if the first character in the string is RTL or not.
Arguments:
str : string
The given string that need to be tested.
Returns: boolean  Whether the first character in str is an RTL char.
code »
goog.i18n.bidi.isRtlExitText(stropt_isHtml) boolean
Use endsWithRtl. Check if the exit directionality a piece of text is RTL, i.e. if the last strongly-directional character in the string is RTL.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether RTL exit directionality was detected.
code »
goog.i18n.bidi.isRtlLanguage(lang) boolean
Check if a BCP 47 / III language code indicates an RTL language, i.e. either: - a language code explicitly specifying one of the right-to-left scripts, e.g. "az-Arab", or

- a language code specifying one of the languages normally written in a right-to-left script, e.g. "fa" (Farsi), except ones explicitly specifying Latin or Cyrillic script (which are the usual LTR alternatives).

The list of right-to-left scripts appears in the 100-199 range in http://www.unicode.org/iso15924/iso15924-num.html, of which Arabic and Hebrew are by far the most widely used. We also recognize Thaana, N'Ko, and Tifinagh, which also have significant modern usage. The rest (Syriac, Samaritan, Mandaic, etc.) seem to have extremely limited or no modern usage and are not recognized to save on code size. The languages usually written in a right-to-left script are taken as those with Suppress-Script: Hebr|Arab|Thaa|Nkoo|Tfng in http://www.iana.org/assignments/language-subtag-registry, as well as Central (or Sorani) Kurdish (ckb), Sindhi (sd) and Uyghur (ug). Other subtags of the language code, e.g. regions like EG (Egypt), are ignored.

Arguments:
lang : string
BCP 47 (a.k.a III) language code.
Returns: boolean  Whether the language code is an RTL language.
code »
goog.i18n.bidi.isRtlText(stropt_isHtml) boolean
Use startsWithRtl. Check whether the first strongly directional character (if any) is RTL.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether RTL directionality is detected using the first strongly-directional character method.
code »
goog.i18n.bidi.mirrorCSS(cssStr) string
Swap location parameters and 'left'/'right' in CSS specification. The processed string will be suited for RTL layout. Though this function can cover most cases, there are always exceptions. It is suggested to put those exceptions in separate group of CSS string.
Arguments:
cssStr : string
CSS spefication string.
Returns: string  Processed CSS specification string.
code »
goog.i18n.bidi.normalizeHebrewQuote(str) string
Replace the double and single quote directly after a Hebrew character with GERESH and GERSHAYIM. In such case, most likely that's user intention.
Arguments:
str : string
String that need to be processed.
Returns: string  Processed string with double/single quote replaced.
code »
goog.i18n.bidi.setElementDirAndAlign(elementdir)
Sets text input element's directionality and text alignment based on a given directionality. Does nothing if the given directionality is unknown or neutral.
Arguments:
element : Element
Input field element to set directionality to.
dir : goog.i18n.bidi.Dir | number | boolean | null
Desired directionality, given in one of the following formats: 1. A goog.i18n.bidi.Dir constant. 2. A number (positive = LRT, negative = RTL, 0 = neutral). 3. A boolean (true = RTL, false = LTR). 4. A null for unknown directionality.
code »
goog.i18n.bidi.startsWithLtr(stropt_isHtml) boolean
Check whether the first strongly directional character (if any) is LTR.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether LTR directionality is detected using the first strongly-directional character method.
code »
goog.i18n.bidi.startsWithRtl(stropt_isHtml) boolean
Check whether the first strongly directional character (if any) is RTL.
Arguments:
str : string
String being checked.
opt_isHtml : boolean=
Whether str is HTML / HTML-escaped. Default: false.
Returns: boolean  Whether RTL directionality is detected using the first strongly-directional character method.
code »
goog.i18n.bidi.stripHtmlIfNeeded_(stropt_isStripNeeded) string
Returns the input text with spaces instead of HTML tags or HTML escapes, if opt_isStripNeeded is true. Else returns the input as is. Useful for text directionality estimation. Note: the function should not be used in other contexts; it is not 100% correct, but rather a good-enough implementation for directionality estimation purposes.
Arguments:
str : string
The given string.
opt_isStripNeeded : boolean=
Whether to perform the stripping. Default: false (to retain consistency with calling functions).
Returns: string  The given string cleaned of HTML tags / escapes.
code »
goog.i18n.bidi.toDir(givenDiropt_noNeutral) ?goog.i18n.bidi.Dir
Convert a directionality given in various formats to a goog.i18n.bidi.Dir constant. Useful for interaction with different standards of directionality representation.
Arguments:
givenDir : goog.i18n.bidi.Dir | number | boolean | null
Directionality given in one of the following formats: 1. A goog.i18n.bidi.Dir constant. 2. A number (positive = LTR, negative = RTL, 0 = neutral). 3. A boolean (true = RTL, false = LTR). 4. A null for unknown directionality.
opt_noNeutral : boolean=
Whether a givenDir of zero or goog.i18n.bidi.Dir.NEUTRAL should be treated as null, i.e. unknown, in order to preserve legacy behavior.
Returns: ?goog.i18n.bidi.Dir  A goog.i18n.bidi.Dir constant matching the given directionality. If given null, returns null (i.e. unknown).
code »

Global Properties

goog.i18n.bidi.FORCE_RTL :
No description.
Code »
goog.i18n.bidi.I18N_LEFT :
'right' if locale is RTL, 'left' if not.
Code »
goog.i18n.bidi.I18N_RIGHT :
'left' if locale is RTL, 'right' if not.
Code »
goog.i18n.bidi.IS_RTL :
Constant that defines whether or not the current locale is a RTL locale. If goog.i18n.bidi.FORCE_RTL is not true, this constant will default to check that goog.LOCALE is one of a few major RTL locales.

This is designed to be a maximally efficient compile-time constant. For example, for the default goog.LOCALE, compiling "if (goog.i18n.bidi.IS_RTL) alert('rtl') else {}" should produce no code. It is this design consideration that limits the implementation to only supporting a few major RTL locales, as opposed to the broader repertoire of something like goog.i18n.bidi.isRtlLanguage.

Since this constant refers to the directionality of the locale, it is up to the caller to determine if this constant should also be used for the direction of the UI.

Code »
goog.i18n.bidi.LEFT :
'left' string constant.
Code »
goog.i18n.bidi.RIGHT :
'right' string constant.
Code »
goog.i18n.bidi.bracketGuardHtmlRe_ :
Regular expression for bracket guard replacement in html.
Code »
goog.i18n.bidi.bracketGuardTextRe_ :
Regular expression for bracket guard replacement in text.
Code »
goog.i18n.bidi.dimensionsRe_ :
Regular expression to find dimensions such as "padding: .3 0.4ex 5px 6;"
Code »
goog.i18n.bidi.doubleQuoteSubstituteRe_ :
Regular expression for hebrew double quote substitution, finding quote directly after hebrew characters.
Code »
goog.i18n.bidi.hasNumeralsRe_ :
Regular expression to check if a string contains any numerals. Used to differentiate between completely neutral strings and those containing numbers, which are weakly LTR.
Code »
goog.i18n.bidi.htmlSkipReg_ :
Simplified regular expression for an HTML tag (opening or closing) or an HTML escape. We might want to skip over such expressions when estimating the text directionality.
Code »
goog.i18n.bidi.isRequiredLtrRe_ :
Regular expression to check if a string looks like something that must always be LTR even in RTL text, e.g. a URL. When estimating the directionality of text containing these, we treat these as weakly LTR, like numbers.
Code »
goog.i18n.bidi.leftRe_ :
Regular expression for left.
Code »
goog.i18n.bidi.ltrCharReg_ :
Regular expression to check for LTR characters.
Code »
goog.i18n.bidi.ltrChars_ :
A practical pattern to identify strong LTR characters. This pattern is not theoretically correct according to the Unicode standard. It is simplified for performance and small code size.
Code »
goog.i18n.bidi.ltrDirCheckRe_ :
Regular expressions to check if a piece of text is of LTR directionality on first character with strong directionality.
Code »
goog.i18n.bidi.ltrExitDirCheckRe_ :
Regular expressions to check if the last strongly-directional character in a piece of text is LTR.
Code »
goog.i18n.bidi.ltrRe_ :
Regular expression pattern to check if the first character in the string is LTR.
Code »
goog.i18n.bidi.rightRe_ :
Regular expression for right.
Code »
goog.i18n.bidi.rtlCharReg_ :
Regular expression to check for RTL characters.
Code »
goog.i18n.bidi.rtlChars_ :
A practical pattern to identify strong RTL character. This pattern is not theoretically correct according to the Unicode standard. It is simplified for performance and small code size.
Code »
goog.i18n.bidi.rtlDetectionThreshold_ :
This constant controls threshold of RTL directionality.
Code »
goog.i18n.bidi.rtlDirCheckRe_ :
Regular expressions to check if a piece of text is of RTL directionality on first character with strong directionality.
Code »
goog.i18n.bidi.rtlExitDirCheckRe_ :
Regular expressions to check if the last strongly-directional character in a piece of text is RTL.
Code »
goog.i18n.bidi.rtlLocalesRe_ :
A regular expression for matching right-to-left language codes. See #isRtlLanguage for the design.
Code »
goog.i18n.bidi.rtlRe_ :
Regular expression pattern to check if the first character in the string is RTL.
Code »
goog.i18n.bidi.singleQuoteSubstituteRe_ :
Regular expression for hebrew single quote substitution, finding quote directly after hebrew characters.
Code »
goog.i18n.bidi.tempRe_ :
Placeholder regular expression for swapping.
Code »
goog.i18n.bidi.wordSeparatorRe_ :
Regular expression to split a string into "words" for directionality estimation based on relative word counts.
Code »

Package i18n

Package Reference