@@ -11,6 +11,21 @@ import {DIR_DOCUMENT} from './dir-document-token';
11
11
12
12
export type Direction = 'ltr' | 'rtl' ;
13
13
14
+ /** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */
15
+ const RTL_LOCALE_PATTERN =
16
+ / ^ ( a r | c k b | d v | h e | i w | f a | n q o | p s | s d | u g | u r | y i | .* [ - _ ] ( A d l m | A r a b | H e b r | N k o o | R o h g | T h a a ) ) (? ! .* [ - _ ] ( L a t n | C y r l ) ( $ | - | _ ) ) ( $ | - | _ ) / i;
17
+
18
+ /** Resolves a string value to a specific direction. */
19
+ export function _resolveDirectionality ( rawValue : string ) : Direction {
20
+ const value = rawValue . toLowerCase ( ) ;
21
+
22
+ if ( value === 'auto' && typeof navigator !== 'undefined' && navigator && navigator . language ) {
23
+ return RTL_LOCALE_PATTERN . test ( navigator . language ) ? 'rtl' : 'ltr' ;
24
+ }
25
+
26
+ return value === 'rtl' ? 'rtl' : 'ltr' ;
27
+ }
28
+
14
29
/**
15
30
* The directionality (LTR / RTL) context for the application (or a subtree of it).
16
31
* Exposes the current direction and a stream of direction changes.
@@ -25,14 +40,9 @@ export class Directionality implements OnDestroy {
25
40
26
41
constructor ( @Optional ( ) @Inject ( DIR_DOCUMENT ) _document ?: any ) {
27
42
if ( _document ) {
28
- // TODO: handle 'auto' value -
29
- // We still need to account for dir="auto".
30
- // It looks like HTMLElemenet.dir is also "auto" when that's set to the attribute,
31
- // but getComputedStyle return either "ltr" or "rtl". avoiding getComputedStyle for now
32
43
const bodyDir = _document . body ? _document . body . dir : null ;
33
44
const htmlDir = _document . documentElement ? _document . documentElement . dir : null ;
34
- const value = bodyDir || htmlDir ;
35
- this . value = value === 'ltr' || value === 'rtl' ? value : 'ltr' ;
45
+ this . value = _resolveDirectionality ( bodyDir || htmlDir || 'ltr' ) ;
36
46
}
37
47
}
38
48
0 commit comments