[change] I18nManager manages global writing direction

This commit is contained in:
Nicolas Gallagher
2016-08-15 11:36:06 -07:00
parent 638479991e
commit fb5406e4ec
3 changed files with 38 additions and 20 deletions
+10 -2
View File
@@ -6,32 +6,40 @@ import I18nManager from '..'
suite('apis/I18nManager', () => {
suite('when RTL not enabled', () => {
setup(() => {
I18nManager.setRTL(false)
I18nManager.setPreferredLanguageRTL(false)
})
test('is "false" by default', () => {
assert.equal(I18nManager.isRTL, false)
assert.equal(document.documentElement.getAttribute('dir'), 'ltr')
})
test('is "true" when forced', () => {
I18nManager.forceRTL(true)
assert.equal(I18nManager.isRTL, true)
assert.equal(document.documentElement.getAttribute('dir'), 'rtl')
I18nManager.forceRTL(false)
})
})
suite('when RTL is enabled', () => {
setup(() => {
I18nManager.setRTL(true)
I18nManager.setPreferredLanguageRTL(true)
})
teardown(() => {
I18nManager.setPreferredLanguageRTL(false)
})
test('is "true" by default', () => {
assert.equal(I18nManager.isRTL, true)
assert.equal(document.documentElement.getAttribute('dir'), 'rtl')
})
test('is "false" when not allowed', () => {
I18nManager.allowRTL(false)
assert.equal(I18nManager.isRTL, false)
assert.equal(document.documentElement.getAttribute('dir'), 'ltr')
I18nManager.allowRTL(true)
})
})
+22 -10
View File
@@ -1,3 +1,5 @@
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment'
type I18nManagerStatus = {
allowRTL: (allowRTL: boolean) => {},
forceRTL: (forceRTL: boolean) => {},
@@ -5,28 +7,38 @@ type I18nManagerStatus = {
isRTL: boolean
}
let isApplicationLanguageRTL = false
let isPreferredLanguageRTL = false
let isRTLAllowed = true
let isRTLForced = false
const isRTL = () => {
if (isRTLForced) {
return true
}
return isRTLAllowed && isPreferredLanguageRTL
}
const onChange = () => {
if (ExecutionEnvironment.canUseDOM) {
document.documentElement.setAttribute('dir', isRTL() ? 'rtl' : 'ltr')
}
}
const I18nManager: I18nManagerStatus = {
allowRTL(bool) {
isRTLAllowed = bool
onChange()
},
forceRTL(bool) {
isRTLForced = bool
onChange()
},
setRTL(bool) {
isApplicationLanguageRTL = bool
setPreferredLanguageRTL(bool) {
isPreferredLanguageRTL = bool
onChange()
},
get isRTL() {
if (isRTLForced) {
return true
}
if (isRTLAllowed && isApplicationLanguageRTL) {
return true
}
return false
return isRTL()
}
}