File size: 20,433 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
import config from '@automattic/calypso-config';
import { captureException } from '@automattic/calypso-sentry';
import { getUrlFromParts, getUrlParts } from '@automattic/calypso-url';
import { isDefaultLocale, getLanguage } from '@automattic/i18n-utils';
import { setLocale as setLocaleNumberFormatters } from '@automattic/number-formatters';
import debugFactory from 'debug';
import i18n from 'i18n-calypso';
import { forEach, throttle } from 'lodash';
const debug = debugFactory( 'calypso:i18n' );
const getPromises = {};
/**
* De-duplicates repeated GET fetches of the same URL while one is taking place.
* Once it's finished, it'll allow for the same request to be done again.
* @param {string} url The URL to fetch
* @returns {Promise} The fetch promise.
*/
function dedupedGet( url ) {
if ( ! ( url in getPromises ) ) {
getPromises[ url ] = globalThis.fetch( url ).finally( () => delete getPromises[ url ] );
}
return getPromises[ url ];
}
/**
* Get the protocol, domain, and path part of the language file URL.
* Normally it should only serve as a helper function for `getLanguageFileUrl`,
* but we export it here still in help with the test suite.
* @returns {string} The path URL to the language files.
*/
export function getLanguageFilePathUrl() {
const protocol = typeof window === 'undefined' ? 'https://' : '//'; // use a protocol-relative path in the browser
return `${ protocol }widgets.wp.com/languages/calypso/`;
}
/**
* Get the base path for language related files that are served from within Calypso.
* @returns {string} The internal base file path for language files.
*/
export function getLanguagesInternalBasePath() {
return `/calypso/languages`;
}
/**
* Get the language file URL for the given locale and file type, js or json.
* A revision cache buster will be appended automatically if `setLangRevisions` has been called beforehand.
* @param {string} localeSlug A locale slug. e.g. fr, jp, zh-tw
* @param {string} fileType The desired file type, js or json. Default to json.
* @param {Object} languageRevisions An optional language revisions map. If it exists, the function will append the revision within as cache buster.
* @returns {string} A language file URL.
*/
export function getLanguageFileUrl( localeSlug, fileType = 'json', languageRevisions = {} ) {
if ( ! [ 'js', 'json' ].includes( fileType ) ) {
fileType = 'json';
}
const fileUrl = `${ getLanguageFilePathUrl() }${ localeSlug }-v1.1.${ fileType }`;
let revision = languageRevisions[ localeSlug ];
if ( typeof revision === 'number' ) {
revision = revision.toString();
}
return typeof revision === 'string' ? fileUrl + `?v=${ revision }` : fileUrl;
}
function getHtmlLangAttribute() {
// translation of this string contains the desired HTML attribute value
const slug = i18n.translate( 'html_lang_attribute' );
// Hasn't been translated? Some languages don't have the translation for this string,
// or maybe we are dealing with the default `en` locale. Return the general purpose locale slug
// -- there's no special one available for `<html lang>`.
if ( slug === 'html_lang_attribute' ) {
return i18n.getLocaleSlug();
}
return slug;
}
function setLocaleInDOM() {
const htmlLangAttribute = getHtmlLangAttribute();
const isRTL = i18n.isRtl();
document.documentElement.lang = htmlLangAttribute;
document.documentElement.dir = isRTL ? 'rtl' : 'ltr';
document.body.classList[ isRTL ? 'add' : 'remove' ]( 'rtl' );
switchWebpackCSS( isRTL );
}
export async function getFile( url ) {
const response = await dedupedGet( url );
if ( response.ok ) {
if ( response.bodyUsed ) {
// If the body was already used, we assume that we already parsed the
// response and set the locale in the DOM, so we don't need to do anything
// else here.
return;
}
return await response.json();
}
// Invalid response.
throw new Error();
}
export function getLanguageFile( targetLocaleSlug ) {
const languageRevisions = typeof window !== 'undefined' ? window.languageRevisions : {};
const url = getLanguageFileUrl( targetLocaleSlug, 'json', languageRevisions );
return getFile( url );
}
/**
* Get the language manifest file URL for the given locale.
* A revision cache buster will be appended automatically if `setLangRevisions` has been called beforehand.
* @param {string} options Funciton options object
* @param {string} options.localeSlug A locale slug. e.g. fr, jp, zh-tw
* @param {string} options.fileType The desired file type, js or json. Default to json.
* @param {string} options.hash Build hash string that will be used as cache buster.
* @returns {string} A language manifest file URL.
*/
export function getLanguageManifestFileUrl( { localeSlug, fileType = 'json', hash = null } = {} ) {
if ( ! [ 'js', 'json' ].includes( fileType ) ) {
fileType = 'json';
}
if ( typeof hash === 'number' ) {
hash = hash.toString();
}
const fileBasePath = getLanguagesInternalBasePath();
const fileUrl = `${ fileBasePath }/${ localeSlug }-language-manifest.${ fileType }`;
return typeof hash === 'string' ? fileUrl + `?v=${ hash }` : fileUrl;
}
/**
* Whether the language manifest is preloaded, i.e. localeSlug is matched in window.i18nLanguageManifest.
* @param {string} localeSlug A locale slug. e.g. fr, jp, zh-tw
* @returns {boolean} Whether the language manifest is preloaded
*/
function getIsLanguageManifestPreloaded( localeSlug ) {
return (
window?.i18nLanguageManifest &&
window?.i18nLanguageManifest?.locale?.[ '' ]?.localeSlug === localeSlug
);
}
/**
* Get the language manifest file for the given locale.
* @param {string} localeSlug A locale slug. e.g. fr, jp, zh-tw
* @returns {Object | Promise} Language manifest json content
*/
export function getLanguageManifestFile( localeSlug ) {
if ( getIsLanguageManifestPreloaded( localeSlug ) ) {
return window.i18nLanguageManifest;
}
const url = getLanguageManifestFileUrl( {
localeSlug,
fileType: 'json',
hash: window?.languageRevisions?.hashes?.[ localeSlug ] || null,
} );
return getFile( url );
}
/**
* Get the translation chunk file URL for the given chunk id and locale.
* A revision cache buster will be appended automatically if `setLangRevisions` has been called beforehand.
* @param {string} options Funciton options object
* @param {string} options.chunkId A chunk id. e.g. chunk-abc.min
* @param {string} options.localeSlug A locale slug. e.g. fr, jp, zh-tw
* @param {string} options.fileType The desired file type, js or json. Default to json.
* @param {string} options.hash Build hash string that will be used as cache buster.
* @returns {string} A translation chunk file URL.
*/
export function getTranslationChunkFileUrl( {
chunkId,
localeSlug,
fileType = 'json',
hash = null,
} = {} ) {
if ( ! [ 'js', 'json' ].includes( fileType ) ) {
fileType = 'json';
}
if ( typeof hash === 'number' ) {
hash = hash.toString();
}
const fileBasePath = getLanguagesInternalBasePath();
const fileName = `${ localeSlug }-${ chunkId }.${ fileType }`;
const fileUrl = `${ fileBasePath }/${ fileName }`;
return typeof hash === 'string' ? fileUrl + `?v=${ hash }` : fileUrl;
}
/**
* Whether the translation chunk is preloaded, i.e. exists in window.i18nTranslationChunks.
* @param {string} chunkId chunkId A chunk id. e.g. chunk-abc.min
* @param {string} localeSlug A locale slug. e.g. fr, jp, zh-tw
* @returns {boolean} Whether the chunk translations are preloaded
*/
function getIsTranslationChunkPreloaded( chunkId, localeSlug ) {
if ( typeof window !== 'undefined' ) {
return (
window.i18nLanguageManifest?.locale?.[ '' ]?.localeSlug === localeSlug &&
window.i18nTranslationChunks &&
chunkId in window.i18nTranslationChunks
);
}
return false;
}
/**
* Get the translation chunk file for the given chunk id and locale.
* @param {string} chunkId A chunk id. e.g. chunk-abc.min
* @param {string} localeSlug A locale slug. e.g. fr, jp, zh-tw
* @returns {Promise} Translation chunk json content
*/
export function getTranslationChunkFile( chunkId, localeSlug ) {
if ( getIsTranslationChunkPreloaded( chunkId, localeSlug ) ) {
return Promise.resolve( window.i18nTranslationChunks[ chunkId ] );
}
const url = getTranslationChunkFileUrl( {
chunkId,
localeSlug,
fileType: 'json',
hash: window?.languageRevisions?.[ localeSlug ] || null,
} );
return getFile( url );
}
/**
* Get the ids of all loaded chunks via either script tag on the page
* or loaded asynchronously with dynamic imports.
* @returns {Array} Chunk ids
*/
function getInstalledChunks() {
const installedChunksFromContext = window.installedChunks ?? [];
const installedChunksAsync = window?.__requireChunkCallback__?.getInstalledChunks?.() ?? [];
const installedChunksSet = new Set(
[].concat( installedChunksFromContext, installedChunksAsync )
);
return Array.from( installedChunksSet );
}
/**
* Capture exceptions from `getTranslationChunkFile`.
* @param {Error} error
* @param {string} chunkId
* @param {string} localeSlug
*/
// eslint-disable-next-line no-unused-vars
function captureGetTranslationChunkFileException( error, chunkId, localeSlug ) {
captureException( error, {
tags: {
chunk_id: chunkId,
locale_slug: localeSlug,
},
} );
}
/**
* Used to keep the reference to the require chunk translations handler.
*/
let lastRequireChunkTranslationsHandler = null;
/**
* Adds require chunk handler for fetching translations.
* @param {string} localeSlug A locale slug. e.g. fr, jp, zh-tw
* @param {Object} options Handler additional options
* @param {Array} options.translatedChunks Array of chunk ids that have available translation for the given locale
* @param {Object} options.userTranslations User translations data that will override chunk translations
*/
function addRequireChunkTranslationsHandler( localeSlug = i18n.getLocaleSlug(), options = {} ) {
const { translatedChunks = [], userTranslations = {} } = options;
const loadedTranslationChunks = {};
const handler = ( { scriptSrc, publicPath }, promises ) => {
const chunkId = scriptSrc.replace( publicPath, '' ).replace( /\.js$/, '' );
if ( ! translatedChunks.includes( chunkId ) || loadedTranslationChunks[ chunkId ] ) {
return;
}
const translationChunkPromise = getTranslationChunkFile( chunkId, localeSlug )
.then( ( translations ) => {
addTranslations( translations, userTranslations );
loadedTranslationChunks[ chunkId ] = true;
} )
.catch( ( cause ) => {
const error = new Error(
`Encountered an error loading translation chunk in require chunk translations handler.`,
{ cause }
);
// @todo: Enable again when figure out how to prevent the spam caused by these errors.
// captureGetTranslationChunkFileException( error, chunkId, localeSlug );
debug( error );
} );
promises.push( translationChunkPromise );
};
window?.__requireChunkCallback__?.add?.( handler );
lastRequireChunkTranslationsHandler = handler;
}
/**
* Removes require chunk translations handler.
*/
function removeRequireChunkTranslationsHandler() {
window?.__requireChunkCallback__?.remove?.( lastRequireChunkTranslationsHandler );
}
let lastRequestedLocale = null;
export default async function switchLocale( localeSlug ) {
// check if the language exists in config.languages
const language = getLanguage( localeSlug );
if ( ! language ) {
return;
}
// Note: i18n is a singleton that will be shared between all server requests!
// Disable switching locale on the server
if ( typeof document === 'undefined' ) {
return;
}
// Propagate the locale to @automattic/number-formatters
setLocaleNumberFormatters( localeSlug );
lastRequestedLocale = localeSlug;
const useTranslationChunks =
config.isEnabled( 'use-translation-chunks' ) ||
getUrlParts( document.location.href ).searchParams.has( 'useTranslationChunks' );
if ( isDefaultLocale( localeSlug ) ) {
i18n.configure( { defaultLocaleSlug: localeSlug } );
setLocaleInDOM();
} else if ( useTranslationChunks ) {
// If requested locale is same as current locale, we don't need to
// re-fetch the manifest and translation chunks.
if ( localeSlug === i18n.getLocaleSlug() ) {
setLocaleInDOM();
return;
}
// Switching the locale requires fetching the translation chunks
// locale data, which consists of the locale manifest data and
// translations for currently installed chunks.
try {
const languageManifest = getLanguageManifestFile( localeSlug );
const { translatedChunks, locale } =
( languageManifest instanceof Promise ? await languageManifest : languageManifest ) ?? {}; // Using await operator on non-Promise object would still split the execution flow which causes unnecessary delay.
if ( ! locale || ! translatedChunks ) {
return;
}
i18n.setLocale( locale );
setLocaleInDOM();
removeRequireChunkTranslationsHandler();
addRequireChunkTranslationsHandler( localeSlug, { translatedChunks } );
const translatedInstalledChunks = getInstalledChunks().filter( ( chunkId ) =>
translatedChunks.includes( chunkId )
);
const preloadedTranslatedInstalledChunks = translatedInstalledChunks.filter( ( chunkId ) =>
getIsTranslationChunkPreloaded( chunkId, localeSlug )
);
const translatedInstalledChunksToBeLoaded = translatedInstalledChunks.filter(
( chunkId ) => ! getIsTranslationChunkPreloaded( chunkId, localeSlug )
);
// Add preloaded translation chunks
const preloadedTranslations = preloadedTranslatedInstalledChunks.reduce(
( acc, chunkId ) => Object.assign( acc, window.i18nTranslationChunks?.[ chunkId ] ),
{}
);
addTranslations( preloadedTranslations );
// Load individual translation chunks
translatedInstalledChunksToBeLoaded.forEach( ( chunkId ) =>
getTranslationChunkFile( chunkId, localeSlug )
.then( ( translations ) => addTranslations( translations ) )
.catch( ( cause ) => {
const error = new Error(
`Encountered an error loading translation chunk while switching the locale.`,
{ cause }
);
// @todo: Enable again when figure out how to prevent the spam caused by these errors.
// captureGetTranslationChunkFileException( error, chunkId, localeSlug );
debug( error );
} )
);
const userTranslations = await loadUserUndeployedTranslations( localeSlug );
// Re-attach require chunk translations handler if user translations are available
if ( userTranslations ) {
removeRequireChunkTranslationsHandler();
addRequireChunkTranslationsHandler( localeSlug, {
translatedChunks,
userTranslations,
} );
}
} catch ( error ) {
debug(
`Encountered an error loading language manifest and/or translation chunks for ${ localeSlug }. Falling back to English.`
);
debug( error );
}
} else {
getLanguageFile( localeSlug ).then(
// Success.
( body ) => {
if ( body ) {
// Handle race condition when we're requested to switch to a different
// locale while we're in the middle of request, we should abandon result
if ( localeSlug !== lastRequestedLocale ) {
return;
}
i18n.setLocale( body );
setLocaleInDOM();
loadUserUndeployedTranslations( localeSlug );
}
},
// Failure.
() => {
debug(
`Encountered an error loading locale file for ${ localeSlug }. Falling back to English.`
);
}
);
}
}
export function loadUserUndeployedTranslations( currentLocaleSlug ) {
if ( typeof window === 'undefined' || ! window.location || ! window.location.search ) {
return;
}
const search = new URLSearchParams( window.location.search );
const params = Object.fromEntries( search.entries() );
const {
'load-user-translations': username,
project = 'wpcom',
translationSet = 'default',
translationStatus = 'current',
locale = currentLocaleSlug,
} = params;
if ( ! username ) {
return;
}
if ( ! [ 'current', 'waiting' ].includes( translationStatus ) ) {
return;
}
if ( 'waiting' === translationStatus ) {
// TODO only allow loading your own waiting translations. Disallow loading them for now.
return;
}
const pathname = [
'api',
'projects',
project,
locale,
translationSet,
'export-translations',
].join( '/' );
const searchParams = new URLSearchParams( {
'filters[user_login]': username,
'filters[status]': translationStatus,
format: 'json',
} );
const requestUrl = getUrlFromParts( {
protocol: 'https:',
host: 'translate.wordpress.com',
pathname,
searchParams,
} );
return window
.fetch( requestUrl.href, {
headers: { Accept: 'application/json' },
credentials: 'include',
} )
.then( ( res ) => res.json() )
.then( ( translations ) => {
addTranslations( translations );
return translations;
} );
}
/*
* CSS links come in two flavors: either RTL stylesheets with `.rtl.css` suffix, or LTR ones
* with `.css` suffix. This function sets a desired `isRTL` flag on the supplied URL, i.e., it
* changes the extension if necessary.
*/
function setRTLFlagOnCSSLink( url, isRTL ) {
if ( isRTL ) {
return url.endsWith( '.rtl.css' ) ? url : url.replace( /\.css$/, '.rtl.css' );
}
return ! url.endsWith( '.rtl.css' ) ? url : url.replace( /\.rtl.css$/, '.css' );
}
/**
* Switch the Calypso CSS between RTL and LTR versions.
* @param {boolean} isRTL True to use RTL css.
*/
export function switchWebpackCSS( isRTL ) {
const currentLinks = document.querySelectorAll( 'link[rel="stylesheet"][data-webpack]' );
forEach( currentLinks, async ( currentLink ) => {
const currentHref = currentLink.getAttribute( 'href' );
const newHref = setRTLFlagOnCSSLink( currentHref, isRTL );
const isNewHrefAdded = currentLink.parentElement?.querySelector( `[href = '${ newHref }']` );
if ( currentHref === newHref || isNewHrefAdded ) {
return;
}
const newLink = await loadCSS( newHref, currentLink );
if ( newLink ) {
newLink.setAttribute( 'data-webpack', true );
currentLink.parentElement?.removeChild( currentLink );
}
} );
}
/**
* Loads a CSS stylesheet into the page.
* @param {string} cssUrl URL of a CSS stylesheet to be loaded into the page
* @param {window.Element} currentLink an existing <link> DOM element before which we want to insert the new one
* @returns {Promise<string>} the new <link> DOM element after the CSS has been loaded
*/
function loadCSS( cssUrl, currentLink ) {
return new Promise( ( resolve ) => {
// While looping the current links the RTL state might have changed
// This is a double-check to ensure the value of isRTL
const isRTL = i18n.isRtl();
const isRTLHref = currentLink.getAttribute( 'href' ).endsWith( '.rtl.css' );
if ( isRTL === isRTLHref ) {
return resolve( null );
}
const link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = cssUrl;
if ( 'onload' in link ) {
link.onload = () => {
link.onload = null;
resolve( link );
};
} else {
// just wait 500ms if the browser doesn't support link.onload
// https://pie.gd/test/script-link-events/
// https://github.com/ariya/phantomjs/issues/12332
setTimeout( () => resolve( link ), 500 );
}
document.head.insertBefore( link, currentLink ? currentLink.nextSibling : null );
} );
}
/**
* Translation data batch strore.
* @type {Array}
*/
const _translationsBatch = [];
/**
* A throttle wrapper around i18n.addTranslations.
*
* This function also saves the duration of the call as a performance measure
* @param {Object} userTranslations User translations data that will override chunk translations
*/
const _addTranslationsBatch = throttle( function ( userTranslations ) {
window.performance?.mark?.( 'add_translations_start' );
i18n.addTranslations( Object.assign( {}, ..._translationsBatch.splice( 0 ), userTranslations ) );
window.performance?.measure?.( 'add_translations', 'add_translations_start' );
window.performance?.clearMarks?.( 'add_translations_start' );
}, 50 );
/**
* Adds new translations to the existing locale data.
* @param {Object} translations Translations data
* @param {Object} [userTranslations] User translations data that will override chunk translations
*/
function addTranslations( translations, userTranslations ) {
_translationsBatch.push( translations );
_addTranslationsBatch( userTranslations );
}
|