Add i18n locale support with 81 translations#421
Draft
cat5inthecradle wants to merge 1 commit intomainfrom
Draft
Add i18n locale support with 81 translations#421cat5inthecradle wants to merge 1 commit intomainfrom
cat5inthecradle wants to merge 1 commit intomainfrom
Conversation
Load non-English translation files from code-dot-org/code-dot-org apps/i18n/fish/ into this repo and wire up runtime locale selection via ?locale=xx_xx query parameter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a Claude written PR. I was curious what it would look like to pull in translations. - Darin
Summary
code-dot-org/code-dot-orgapps/i18n/fish/intoi18n/locales/?locale=xx_xxquery parameter for the standalone demoinitAll()API to accept raw translation JSON alongside the existing pre-compiled function override pathbin/syncTranslations.jsscript for keeping translations in sync with the monorepoMotivation
We are interested in hosting this activity in an iframe and needs localization support. The translations already exist in the main code-dot-org monorepo but weren't accessible to standalone consumers of this repo.
Technical decisions
Runtime fetch vs. build-time bundling for locale files
Locale JSON files are fetched at runtime (
fetch('locales/es_es.json')) rather than bundled into the main JS bundle. Bundling all 81 locales (~587KB of JSON) would bloatoceans.jsfor every user when only one locale is needed per session. Runtime fetch means a single small (7-20KB) request for just the locale needed, and English users pay nothing.Tradeoff: There's a brief async gap before translations load. In practice the JSON files are tiny and same-origin so this is negligible.
Raw strings vs. pre-compiled function detection in
initI18nThe original
initI18nassumed overrides were pre-compiled MessageFormat functions (as the main code-dot-org host app provides). A type check was added —typeof values[0] === 'string'— to auto-compile raw JSON strings when detected.This preserves backward compatibility: the host app integration path (passing pre-compiled functions) still works unchanged, while the standalone demo and new iframe embedders can pass raw JSON translation objects directly.
Locale code extraction:
es_es→esTranslation files use
xx_xxnaming (e.g.es_es,pt_br) but MessageFormat v2.3.0's constructor expects a BCP 47 language code likeesto select pluralization rules. The language portion is split from the locale and passed to MessageFormat.Regional variants (
pt_brvspt_pt) share pluralization rules, so this is correct — the actual translated strings remain distinct per file.Locale files as CopyPlugin static assets
Locale files are added to the existing
CopyPluginconfig rather than processed as webpack modules. They need to be individually addressable by URL at runtime for the fetch. CopyPlugin copies them verbatim todist/locales/, which is exactly what's needed — webpack module processing would either bundle them all or create content-hashed chunk filenames that runtime fetch couldn't predict.?locale=query parameterFollows the existing convention of the standalone demo page, which already uses query parameters for configuration (
?mode=,?guides=,?tts=). This is the standard approach for passing config into iframe-embedded apps — stateless, visible, and controlled entirely by the embedder via thesrcattribute.Graceful fallback on fetch failure
If a locale file doesn't exist or fetch fails, the app logs a warning and initializes in English. The
?locale=param comes from URL input that could be a typo or unsupported locale — crashing the activity over it would be a poor experience.Usage
localhost:8080localhost:8080?locale=es_es<iframe src="https://host/index.html?locale=fr_fr">initAll({i18n: translationJSON, locale: 'fr'})yarn sync-i18n(GitHub) oryarn sync-i18n:local ~/code/code-dot-orgTest plan
yarn dev— app loads atlocalhost:8080in English (no regression)localhost:8080?locale=es_es— Spanish strings appearlocalhost:8080?locale=fr_fr— French strings appearlocalhost:8080?locale=bogus— falls back to English with console warning?localeparam — English works as before (backward compat)yarn build— production build succeeds, locale files appear indist/locales/🤖 Generated with Claude Code