1import { Localization } from 'expo'; 2import { Localization as DangerZoneLocalization } from 'expo/DangerZone'; 3import i18n from 'i18n-js'; 4 5const en = { 6 foo: 'Nave', 7 bar: 'Nocab', 8}; 9 10const fr = { 11 foo: 'Nocab', 12 bar: 'Nave', 13}; 14 15export const name = 'Localization'; 16 17export function test(t) { 18 t.describe(`Localization methods`, () => { 19 t.it('expect async to return locale', async () => { 20 function validateString(result) { 21 t.expect(result).toBeDefined(); 22 t.expect(typeof result).toBe('string'); 23 t.expect(result.length > 0).toBe(true); 24 } 25 26 function validateStringArray(result) { 27 t.expect(result).toBeDefined(); 28 t.expect(Array.isArray(result)).toBe(true); 29 t.expect(result.length > 0).toBe(true); 30 } 31 32 const { 33 locale, 34 locales, 35 timezone, 36 isoCurrencyCodes, 37 country, 38 isRTL, 39 } = await Localization.getLocalizationAsync(); 40 41 validateString(locale); 42 validateString(timezone); 43 validateString(country); 44 45 validateStringArray(isoCurrencyCodes); 46 validateStringArray(locales); 47 t.expect(locales[0]).toBe(Localization.locale); 48 t.expect(typeof isRTL).toBe('boolean'); 49 }); 50 }); 51 52 t.describe(`Localization defines constants`, () => { 53 t.it('Gets the current device country', async () => { 54 const result = Localization.country; 55 56 t.expect(result).toBeDefined(); 57 t.expect(typeof result).toBe('string'); 58 t.expect(result.length > 0).toBe(true); 59 }); 60 t.it('Gets the current locale', async () => { 61 const result = Localization.locale; 62 63 t.expect(result).toBeDefined(); 64 t.expect(typeof result).toBe('string'); 65 t.expect(result.length > 0).toBe(true); 66 }); 67 t.it('Gets the preferred locales', async () => { 68 const result = Localization.locales; 69 70 t.expect(result).toBeDefined(); 71 t.expect(Array.isArray(result)).toBe(true); 72 t.expect(result.length > 0).toBe(true); 73 t.expect(result[0]).toBe(Localization.locale); 74 }); 75 t.it('Gets ISO currency codes', async () => { 76 const result = Localization.isoCurrencyCodes; 77 t.expect(result).toBeDefined(); 78 t.expect(Array.isArray(result)).toBe(true); 79 t.expect(result.length > 0).toBe(true); 80 for (let iso of result) { 81 t.expect(typeof iso).toBe('string'); 82 t.expect(iso.length > 0).toBe(true); 83 } 84 }); 85 t.it('Gets the current timzezone', async () => { 86 const result = Localization.timezone; 87 t.expect(result).toBeDefined(); 88 t.expect(typeof result).toBe('string'); 89 t.expect(result.length > 0).toBe(true); 90 // Format: expect something like America/Los_Angeles or America/Chihuahua 91 t.expect(result.split('/').length > 1).toBe(true); 92 }); 93 94 t.it('Gets the current layout direction (ltr only)', async () => { 95 const result = Localization.isRTL; 96 t.expect(result).toBeDefined(); 97 t.expect(result).toBe(false); 98 }); 99 }); 100 101 t.describe(`Localization works with i18n-js`, () => { 102 i18n.locale = Localization.locale; 103 i18n.translations = { en, fr }; 104 i18n.missingTranslationPrefix = 'EE: '; 105 i18n.fallbacks = true; 106 107 t.it('expect language to match strings (en only)', async () => { 108 const target = 'foo'; 109 110 i18n.locale = Localization.locale; 111 112 const expoPredictedLangTag = Localization.locale.split('-')[0]; 113 const translation = i18n.translations[expoPredictedLangTag]; 114 115 t.expect(translation[target]).toBe(i18n.t(target)); 116 }); 117 }); 118 119 t.describe(`DangerZone.Localization`, () => { 120 const currentWarn = console.warn; 121 122 t.beforeEach(() => { 123 console.warn = currentWarn; 124 }); 125 126 [ 127 ['getCurrentDeviceCountryAsync', 'country'], 128 ['getCurrentLocaleAsync', 'locale'], 129 ['getCurrentTimeZoneAsync', 'timezone'], 130 ['getPreferredLocalesAsync', 'locales'], 131 ['getISOCurrencyCodesAsync', 'isoCurrencyCodes'], 132 ].forEach(obj => { 133 const [deprecated, replacement] = obj; 134 135 t.it(`${deprecated} is deprecated`, async () => { 136 const target = `Expo.DangerZone.Localization.${deprecated}() is deprecated. Use \`Expo.Localization.${replacement}\` instead.`; 137 138 let warning = null; 139 console.warn = (...props) => { 140 warning = props[0]; 141 console.warn = currentWarn; 142 }; 143 144 const value = await DangerZoneLocalization[deprecated](); 145 146 t.expect(warning).toBe(target); 147 t.expect(value + '').toBe(Localization[replacement] + ''); 148 }); 149 }); 150 }); 151} 152