-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Finally we're here: you can just set `language="es"` on the calculator element and it'll be in Spanish. Getting the strings out of the XLIFF file and into code requires running `lit-localize build`. Rather than making people do that manually, I wrote a Parcel resolver that runs the command behind the scenes. Overengineered? Maybe! But I learned a bunch about Parcel! Note about switching `language` dynamically. Apart from the problem I noted in the comment on the `language` attribute (text that came back from the API will not change language until the next API fetch), there's another problem, with the Shoelace select elements: the text shown in them won't change until you make a new selection in the element. I _think_ it may be related to [this](shoelace-style/shoelace#1570); in any case, once all the immediate i18n work is wrapped up I may try to isolate the issue and file an issue with them if it's not the same bug. With this bug, dynamically setting the attribute _on page load_ will leave a couple of untranslated strings, and I think that's a use case we do want to support. ## Test Plan Add a `language="es"` attribute to the main element in `rhode-island.html`, and make sure the UI shows up in Spanish. Query for incentives; make sure the program names of federal incentives show up in Spanish. (Those are the only thing localized on the backend right now.)
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 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 |
---|---|---|
|
@@ -29,3 +29,6 @@ yarn-error.log* | |
# cypress | ||
cypress/videos/*.mp4 | ||
cypress/screenshots/* | ||
|
||
# lit-localize generated | ||
generated |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"extends": ["@parcel/config-default"], | ||
"reporters": ["...", "parcel-reporter-static-files-copy"] | ||
"resolvers": ["./scripts/parcel-resolver-locales.mjs", "..."], | ||
"reporters": ["...", "parcel-reporter-static-files-copy"] | ||
} |
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,44 @@ | ||
import { Resolver } from '@parcel/plugin'; | ||
import { exec } from 'child_process'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { promisify } from 'util'; | ||
|
||
async function allXlfFiles(projectRoot) { | ||
const entries = await promisify(fs.readdir)( | ||
path.join(projectRoot, 'translations'), | ||
); | ||
return entries.map(entry => path.join(projectRoot, 'translations', entry)); | ||
} | ||
|
||
/** | ||
* Resolves import specifiers starting with `locales:` by running lit-localize | ||
* to generate strings files, and pointing Parcel at the generated files. | ||
* | ||
* NB: this is not a Typescript file! (Parcel doesn't support plugins written | ||
* in TS.) No type checking! | ||
*/ | ||
export default new Resolver({ | ||
async resolve({ specifier, options: { projectRoot } }) { | ||
if (specifier.startsWith('locales:')) { | ||
await promisify(exec)('npx lit-localize build'); | ||
|
||
const locale = specifier.substring('locales:'.length); | ||
const filePath = | ||
locale === 'config' | ||
? path.join(projectRoot, 'generated/locales.ts') | ||
: path.join(projectRoot, `generated/strings/${locale}.ts`); | ||
|
||
return { | ||
// Rebuild if an XLIFF file changes, or the lit-localize config. | ||
invalidateOnFileChange: [ | ||
...(await allXlfFiles(projectRoot)), | ||
path.join(projectRoot, 'lit-localize.json'), | ||
], | ||
filePath, | ||
}; | ||
} else { | ||
return null; | ||
} | ||
}, | ||
}); |
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
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