// WARN: This was not here, before FE-2451. Test whether adding it will break anything // 'use strict'; /** Supported languages/locales for the website. * * This is placed here so that we have a single place in JavaScript to store this information. */ export const languageToLocale = { /* eslint-disable id-length */ en: 'en_US', fr: 'fr_FR', pt: 'pt_BR', nl: 'nl_NL', es: 'es_ES', de: 'de_DE', zh: 'zh_CN', tr: 'tr_TR', /* eslint-enable */ }; export const languages = Object.keys(languageToLocale); export const locales = Object.values(languageToLocale); /** Extract the language from the locale string (the first two characters) * @param {string} locale - A known locale written in a specific format * @return {string} A known language name written in a specific format * @todo Consider creating an `@external` for two-digit locales * @todo Consider creating a `@typedef` for our language syntax */ export function languageFromLocale(locale) { return locale.substr(0, 2).toLowerCase(); } /** Extract the language prefix and return it or a default * @param {external:Location.pathname} requestURI - The URI from which to extract the language * @return {boolan} A valid language */ export function languageFromRequestUri(requestURI) { const pattern = RegExp('^\/([a-z]{2})(?:|\b)'); // eslint-disable-line no-control-regex, no-useless-escape const matches = requestURI.match(pattern); const hasMatches = (matches !== null); const lang = (hasMatches) ? matches[1] : 'no-language-found'; const defaultLang = 'en'; // FE-2673/FE-2674: Use `includes`, instead of `indexOf`. // - Add `!String.includes` to list in `…/templates-hidden/_site.js.html`. const hasLanguage = (hasMatches && (languages.indexOf(lang) > -1)); return (hasLanguage) ? lang : defaultLang; } /** Set the canonical path, so google analytics page views are tracked properly * @param {external:Location.pathname} pathname - The URI of the current page * @return {string} The path with the language code removed */ export function getCanonicalPath(pathname) { let path = pathname; const language = languageFromRequestUri(pathname); if (language !== 'en') { path = path.substring( 1 + language.length); // The `1` represents the length of the starting slash `/` of a path if (path === '') { path = '/'; } } return path; }