goog.i18n.bidi
Functions
detectRtlDirectionality( str, opt_isHtml ) → boolean
boolean
Check the directionality of a piece of text, return true if the piece of text should be laid out in RTL direction.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
endsWithLtr( str, opt_isHtml ) → boolean
boolean
endsWithRtl( str, opt_isHtml ) → boolean
boolean
enforceLtrInHtml( html ) → string
string
Enforce the html snippet in RTL directionality regardless or 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 starts with a 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).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
enforceLtrInText( text ) → string
string
enforceRtlInHtml( html ) → string
string
Enforce the html snippet in RTL directionality regardless of 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 starts with a 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).
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
enforceRtlInText( text ) → string
string
estimateDirection( str, opt_isHtml ) → goog.i18n.bidi.Dir
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.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
guardBracketInText( s, opt_isRtlContext ) → string
string
Apply bracket guard using LRM and RLM. This is to address the problem of messy bracket display frequently happens in RTL layout. This function works for plain text, not for HTML. In HTML, the opening bracket might be in a different context than the closing bracket (such as an attribute value).
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
hasAnyLtr( str, opt_isHtml ) → boolean
boolean
hasAnyRtl( str, opt_isHtml ) → boolean
boolean
hasRtlChar( str ) → boolean
boolean
isLtrChar( str ) → boolean
boolean
isLtrExitText( str, opt_isHtml ) → boolean
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.
warning Deprecated | Use endsWithLtr. |
---|
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
isLtrText( str, opt_isHtml ) → boolean
boolean
Check whether the first strongly directional character (if any) is LTR.
warning Deprecated | Use startsWithLtr. |
---|
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
isNeutralChar( str ) → boolean
boolean
isNeutralText( str, opt_isHtml ) → boolean
boolean
isRtlChar( str ) → boolean
boolean
isRtlExitText( str, opt_isHtml ) → boolean
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.
warning Deprecated | Use endsWithRtl. |
---|
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
isRtlLanguage( lang ) → boolean
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, and N'Ko, which also have significant modern usage. Adlam and Rohingya scripts are now included since they can be expected to be used in the future. 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|Adlm|Rohg 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.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
isRtlText( str, opt_isHtml ) → boolean
boolean
Check whether the first strongly directional character (if any) is RTL.
warning Deprecated | Use startsWithRtl. |
---|
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
mirrorCSS( cssStr ) → string
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.
Parameters |
| ||||
---|---|---|---|---|---|
Returns |
|
normalizeHebrewQuote( str ) → string
string
setElementDirAndAlign( element, dir ) → void
void
Sets text input element's directionality and text alignment based on a given directionality. Does nothing if the given directionality is unknown or neutral.
Parameters |
|
---|
setElementDirByTextDirectionality( element, text ) → void
void
Sets element dir based on estimated directionality of the given text.
Parameters |
|
---|
startsWithLtr( str, opt_isHtml ) → boolean
boolean
startsWithRtl( str, opt_isHtml ) → boolean
boolean
toDir( givenDir, opt_noNeutral ) → (goog.i18n.bidi.Dir|null)
(goog.i18n.bidi.Dir|null)
Convert a directionality given in various formats to a goog.i18n.bidi.Dir constant. Useful for interaction with different standards of directionality representation.
Parameters |
| ||||||||
---|---|---|---|---|---|---|---|---|---|
Returns |
|
Properties
I18N_LEFT → string
string
'right' if locale is RTL, 'left' if not.
I18N_RIGHT → string
string
'left' if locale is RTL, 'right' if not.
IS_RTL → boolean
boolean
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.
goog.LOCALE
LEFT → string
string
'left' string constant.
RIGHT → string
string
'right' string constant.
Compiler Constants
goog.i18n.bidi.FORCE_RTL → boolean
boolean
FORCE_RTL forces the goog.i18n.bidi.IS_RTL
constant
to say that the current locale is a RTL locale. This should only be used
if you want to override the default behavior for deciding whether the
current locale is RTL or not.
goog.i18n.bidi.IS_RTL