-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
312 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/framework/ui/support/services/i18n/i18nLayout.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
I18nManager, | ||
ViewStyle, | ||
} from 'react-native'; | ||
import { I18nLayoutFlexMap } from './i18nLayoutFlexMap'; | ||
import { I18nLayoutServiceType } from './type'; | ||
|
||
class NativeI18nLayoutService implements I18nLayoutServiceType { | ||
|
||
public isRTL(): boolean { | ||
return I18nManager.isRTL; | ||
} | ||
|
||
public select<T>(ltr: T, rtl): T { | ||
return this.isRTL() ? rtl : ltr; | ||
} | ||
|
||
/** | ||
* Iterates through I18nLayoutFlexMap and reverses style values if needed. | ||
* | ||
* @param {ViewStyle} source - style to convert | ||
* @param {boolean} rtl - is layout currently in RTL mode (Needed for tests, because unable to mock this) | ||
* | ||
* @returns {ViewStyle} - style reversed to fit i18n | ||
*/ | ||
public toI18nStyle(source: ViewStyle, rtl: boolean = this.isRTL()): ViewStyle { | ||
const i18nStyle: ViewStyle = Object.keys(I18nLayoutFlexMap).reduce((style: ViewStyle, prop: string): ViewStyle => { | ||
const currentStyleValue = source[prop]; | ||
if (currentStyleValue) { | ||
const i18nStyleValue = I18nLayoutFlexMap[prop].toI18n(currentStyleValue, rtl); | ||
return { ...style, [prop]: i18nStyleValue }; | ||
} | ||
|
||
return style; | ||
}, {}); | ||
|
||
return { ...source, ...i18nStyle }; | ||
} | ||
} | ||
|
||
export const I18nLayoutService = new NativeI18nLayoutService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ViewStyle } from 'react-native'; | ||
import { I18nLayoutService } from './i18nLayout.service'; | ||
|
||
describe('@i18n-layout: service checks', () => { | ||
|
||
it('* creates LTR style properly', () => { | ||
const i18nStyle: ViewStyle = I18nLayoutService.toI18nStyle({ | ||
alignContent: 'flex-start', | ||
alignItems: 'flex-end', | ||
alignSelf: 'flex-start', | ||
justifyContent: 'flex-end', | ||
flexDirection: 'row', | ||
flexWrap: 'wrap-reverse', | ||
}, false); | ||
|
||
expect(i18nStyle).toEqual({ | ||
alignContent: 'flex-start', | ||
alignItems: 'flex-end', | ||
alignSelf: 'flex-start', | ||
justifyContent: 'flex-end', | ||
flexDirection: 'row', | ||
flexWrap: 'wrap-reverse', | ||
}); | ||
}); | ||
|
||
it('* creates RTL style properly', () => { | ||
const i18nStyle: ViewStyle = I18nLayoutService.toI18nStyle({ | ||
alignContent: 'flex-start', | ||
alignItems: 'flex-end', | ||
alignSelf: 'flex-start', | ||
justifyContent: 'flex-end', | ||
flexDirection: 'row', | ||
flexWrap: 'wrap-reverse', | ||
}, true); | ||
|
||
expect(i18nStyle).toEqual({ | ||
alignContent: 'flex-end', | ||
alignItems: 'flex-start', | ||
alignSelf: 'flex-end', | ||
justifyContent: 'flex-start', | ||
flexDirection: 'row-reverse', | ||
flexWrap: 'wrap', | ||
}); | ||
}); | ||
|
||
it('* creates RTL style properly - partial', () => { | ||
const i18nStyle: ViewStyle = I18nLayoutService.toI18nStyle({ | ||
alignItems: 'flex-end', | ||
justifyContent: 'flex-end', | ||
flexDirection: 'row', | ||
}, true); | ||
|
||
expect(i18nStyle).toEqual({ | ||
alignItems: 'flex-start', | ||
justifyContent: 'flex-start', | ||
flexDirection: 'row-reverse', | ||
}); | ||
}); | ||
|
||
}); |
Oops, something went wrong.