-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a prototype for loading fluent strings, see phetsims/joist#992
- Loading branch information
1 parent
eda6586
commit de7987b
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024, University of Colorado Boulder | ||
|
||
/** | ||
* A PROTOTYPE for loading unbuilt fluent strings. Meant to be a preload script, pulling in | ||
* the fluent strings before the simulation is loaded. | ||
* | ||
* This will need to be integrated into PhET's other string loading in a more general way. | ||
* | ||
* @author Jesse Greenberg (PhET Interactive Simulations) | ||
*/ | ||
|
||
( () => { | ||
window.phet = window.phet || {}; | ||
window.phet.chipper = window.phet.chipper || {}; | ||
|
||
const repoName = phet.chipper.packageObject.name; | ||
|
||
// Constructing the string map | ||
window.phet.chipper.fluentStrings = {}; | ||
|
||
const requestFluentFile = locale => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open( 'GET', `${repoName}-strings_${locale}.ftl`, true ); | ||
xhr.responseType = 'text'; | ||
xhr.onload = () => { | ||
if ( xhr.status === 200 ) { | ||
window.phet.chipper.fluentStrings[ locale ] = xhr.response; | ||
} | ||
}; | ||
|
||
xhr.onerror = () => { | ||
|
||
// Handle network errors gracefully | ||
console.warn( `Network error while requesting fluent file for locale ${locale}` ); | ||
}; | ||
|
||
xhr.send(); | ||
}; | ||
|
||
// TODO: https://github.com/phetsims/joist/issues/992 | ||
// How to get these? I tried using Object.keys( phet.chipper.strings ) (like availableRuntimeLocales), but that | ||
// results in many 404 errors when there ins't a file to load. load-unbuilt-strings.js pulls from babel but I | ||
// didn't undersetand that. And this prototype pulls files from the sim repo for now. These also don't | ||
// support fallback locales appropriately. | ||
const localeList = [ 'en', 'es' ]; | ||
|
||
localeList.forEach( locale => { | ||
requestFluentFile( locale ); | ||
} ); | ||
} )(); |