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