|
| 1 | +import root from 'window-or-global'; |
| 2 | +import axios from 'axios'; |
| 3 | +import { ipcinfoCookie, geolocation } from '@carbon/ibmdotcom-utilities'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @constant {string | string} Host for the Locale API call |
| 7 | + * @private |
| 8 | + */ |
| 9 | +const _host = process.env.TRANSLATION_HOST || 'https://www.ibm.com'; |
| 10 | + |
| 11 | +/** |
| 12 | + * @constant {string | string} CORS proxy for lower environment calls |
| 13 | + * @private |
| 14 | + */ |
| 15 | +const _proxy = process.env.CORS_PROXY || ''; |
| 16 | + |
| 17 | +/** |
| 18 | + * Locale API endpoint |
| 19 | + * |
| 20 | + * @type {string} |
| 21 | + * @private |
| 22 | + */ |
| 23 | +const _endpoint = `${_host}/common/v18/js/data/jsononly/locale`; |
| 24 | + |
| 25 | +/** |
| 26 | + * Locale API class with method of fetching user's locale for |
| 27 | + * ibm.com |
| 28 | + */ |
| 29 | +class LocaleAPI { |
| 30 | + /** |
| 31 | + * Gets the user's locale |
| 32 | + * |
| 33 | + * check if ipcinfo cookie exists (ipcinfoCookie util) |
| 34 | + * if not, retrieve the user's locale through geolocation util + gets user's |
| 35 | + * browser language preference then set the cookie |
| 36 | + * |
| 37 | + * @returns {object} object with lc and cc |
| 38 | + * @example |
| 39 | + * import { LocaleAPI } from '@carbon/ibmdotcom-services'; |
| 40 | + * |
| 41 | + * async function getLocale() { |
| 42 | + * const locale = await LocaleAPI.getLocale(); |
| 43 | + * return locale; |
| 44 | + * } |
| 45 | + */ |
| 46 | + static async getLocale() { |
| 47 | + const cookie = ipcinfoCookie.get(); |
| 48 | + if (cookie && cookie.cc && cookie.lc) { |
| 49 | + return cookie; |
| 50 | + } else { |
| 51 | + const cc = await geolocation(); |
| 52 | + |
| 53 | + /** |
| 54 | + * get language preference from browser |
| 55 | + * returns in en-US format so will need to extract language only |
| 56 | + */ |
| 57 | + const lang = root.navigator.language; |
| 58 | + |
| 59 | + const lc = lang.split('-')[0]; |
| 60 | + |
| 61 | + if (cc && lc) { |
| 62 | + const list = await this.getList(); |
| 63 | + const verifiedCodes = this.verifyLocale(cc, lc, list); |
| 64 | + |
| 65 | + // set the ipcInfo cookie |
| 66 | + ipcinfoCookie.set(verifiedCodes); |
| 67 | + |
| 68 | + return verifiedCodes; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get the country list of all supported countries and their languages |
| 75 | + * |
| 76 | + * @returns {Promise <any>} promise object |
| 77 | + * |
| 78 | + * @example |
| 79 | + * import { LocaleAPI } from '@carbon/ibmdotcom-services'; |
| 80 | + * |
| 81 | + * function async getLocale() { |
| 82 | + * const list = await LocaleAPI.getList(); |
| 83 | + * } |
| 84 | + */ |
| 85 | + static async getList() { |
| 86 | + const url = `${_proxy}${_endpoint}/usen-locale.json`; |
| 87 | + |
| 88 | + return await axios |
| 89 | + .get(url, { |
| 90 | + headers: { |
| 91 | + 'Content-Type': 'application/json; charset=utf-8', |
| 92 | + }, |
| 93 | + }) |
| 94 | + .then(response => response.data); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Verify that the cc and lc combo is in the list of |
| 99 | + * supported cc-lc combos |
| 100 | + * |
| 101 | + * @param {string} cc country code |
| 102 | + * @param {string} lc language code |
| 103 | + * @param {object} list country list |
| 104 | + * |
| 105 | + * @returns {object} object with lc and cc |
| 106 | + * @example |
| 107 | + * import { LocaleAPI } from '@carbon/ibmdotcom-services'; |
| 108 | + * |
| 109 | + * async function getLocale() { |
| 110 | + * const cc = 'us'; |
| 111 | + * const lc = 'en' |
| 112 | + * const locale = await LocaleAPI.verifyLocale(cc, lc, data); |
| 113 | + * return locale; |
| 114 | + * } |
| 115 | + */ |
| 116 | + static verifyLocale(cc, lc, list) { |
| 117 | + let priorityLC; |
| 118 | + let locale; |
| 119 | + |
| 120 | + const language = |
| 121 | + list && |
| 122 | + list.regionList.forEach(region => |
| 123 | + region.countryList.forEach(country => { |
| 124 | + const code = country.locale[0][0].split('-'); |
| 125 | + const countryCode = code[1]; |
| 126 | + const languageCode = code[0]; |
| 127 | + if (countryCode === cc && languageCode === lc) { |
| 128 | + locale = { cc, lc }; |
| 129 | + } |
| 130 | + // save the priority language associated with the user's country code |
| 131 | + else if (countryCode === cc && !priorityLC) { |
| 132 | + priorityLC = languageCode; |
| 133 | + } |
| 134 | + }) |
| 135 | + ); |
| 136 | + if (!language && priorityLC) { |
| 137 | + locale = { cc, lc: priorityLC }; |
| 138 | + } |
| 139 | + return locale; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +export default LocaleAPI; |
0 commit comments