1import * as Localization from 'expo-localization'; 2import i18n from 'i18n-js'; 3import { Platform } from 'react-native'; 4 5const en = { 6 good: 'good', 7 morning: 'morning', 8}; 9 10const fr = { 11 good: 'bien', 12 morning: 'matin', 13}; 14 15const pl = { 16 good: 'dobry', 17 morning: 'rano', 18}; 19 20export const name = 'Localization'; 21 22export function test(t) { 23 t.describe(`Localization methods`, () => { 24 t.it('expect async to return locale', async () => { 25 function validateString(result) { 26 t.expect(result).toBeDefined(); 27 t.expect(typeof result).toBe('string'); 28 t.expect(result.length > 0).toBe(true); 29 } 30 31 function validateStringArray(result) { 32 t.expect(result).toBeDefined(); 33 t.expect(Array.isArray(result)).toBe(true); 34 } 35 36 const { 37 locale, 38 locales, 39 timezone, 40 isoCurrencyCodes, 41 country, 42 isRTL, 43 } = await Localization.getLocalizationAsync(); 44 45 validateString(locale); 46 validateString(timezone); 47 validateString(country); 48 49 validateStringArray(isoCurrencyCodes); 50 validateStringArray(locales); 51 t.expect(locales[0]).toBe(Localization.locale); 52 t.expect(typeof isRTL).toBe('boolean'); 53 }); 54 }); 55 56 t.describe(`Localization defines constants`, () => { 57 t.it('Gets the current device country', async () => { 58 const result = Localization.country; 59 60 t.expect(result).toBeDefined(); 61 t.expect(typeof result).toBe('string'); 62 t.expect(result.length > 0).toBe(true); 63 }); 64 t.it('Gets the current locale', async () => { 65 const result = Localization.locale; 66 67 t.expect(result).toBeDefined(); 68 t.expect(typeof result).toBe('string'); 69 t.expect(result.length > 0).toBe(true); 70 }); 71 t.it('Gets the preferred locales', async () => { 72 const result = Localization.locales; 73 74 t.expect(result).toBeDefined(); 75 t.expect(Array.isArray(result)).toBe(true); 76 t.expect(result.length > 0).toBe(true); 77 t.expect(result[0]).toBe(Localization.locale); 78 }); 79 t.it('Gets ISO currency codes', async () => { 80 const result = Localization.isoCurrencyCodes; 81 t.expect(result).toBeDefined(); 82 t.expect(Array.isArray(result)).toBe(true); 83 for (let iso of result) { 84 t.expect(typeof iso).toBe('string'); 85 t.expect(iso.length > 0).toBe(true); 86 } 87 }); 88 if (Platform.OS !== 'web') { 89 t.it('Gets the current timezone', async () => { 90 const result = Localization.timezone; 91 t.expect(result).toBeDefined(); 92 t.expect(typeof result).toBe('string'); 93 t.expect(result.length > 0).toBe(true); 94 // Format: expect something like America/Los_Angeles or America/Chihuahua 95 t.expect(result.split('/').length > 1).toBe(true); 96 }); 97 } 98 99 t.it('Gets the current layout direction (ltr only)', async () => { 100 const result = Localization.isRTL; 101 t.expect(result).toBeDefined(); 102 t.expect(result).toBe(false); 103 }); 104 }); 105 106 t.describe(`Localization works with i18n-js`, () => { 107 i18n.locale = Localization.locale; 108 i18n.translations = { en, fr, pl }; 109 i18n.missingTranslationPrefix = 'EE: '; 110 i18n.fallbacks = true; 111 112 t.it('expect language to match strings (en, pl, fr supported)', async () => { 113 const target = 'good'; 114 115 i18n.locale = Localization.locale; 116 117 const expoPredictedLangTag = Localization.locale.split('-')[0]; 118 const translation = i18n.translations[expoPredictedLangTag]; 119 120 t.expect(translation[target]).toBe(i18n.t(target)); 121 }); 122 }); 123} 124