Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break dependency on lit/localize #126

Merged
merged 3 commits into from
Feb 9, 2024
Merged

Break dependency on lit/localize #126

merged 3 commits into from
Feb 9, 2024

Commits on Feb 9, 2024

  1. Break dependency on lit/localize

    ## Description
    
    This is unfortunately a huge PR, so here's a detailed guide.
    
    Using `@lit/localize` at runtime also entails depending on the
    entirety of Lit; the localize package is not factored so that you can
    just use the runtime string-substitution part. So we gotta get rid of it.
    
    The idea here was to be minimally disruptive by simply swapping out
    the `msg` and `str` functions from lit-localize with our own
    implementations.  That's in `src/i18n/use-translated.ts` and
    `src/i18n/str.ts`. The `strings:build` command now also invokes sed to
    adjust the import in the generated translated-strings files.
    
    Rather than putting the current locale in global state like Lit does,
    I put it in React context, accessed via the new custom hook
    `useTranslated`.  The hook returns the `msg` function, bound to the
    current locale; callsites then use that msg function as before.
    
    Most of the changed files are just plumbing in the new hook and/or msg
    function. The most substantive changes are in:
    
    - `state-calculator.tsx` where the locale context is set
    
    - `calculator.tsx` -- this is the old embed, which was never actually
      localized; I removed all the `msg` calls here since we're getting
      rid of it Soon™
    
    - `i18n/use-translated.ts` -- the core of the new stuff
    
    ## Test Plan
    
    - Run `yarn strings:extract` and make sure there are no changes to es.xlf.
    
    - Run `yarn strings:build` and make sure there are no changes.
    
    - View the calculator and hit Calculate; make sure all strings come up
      looking correct in English. Add the `lang="es"` attribute to the
      calculator element and make sure the UI immediately switches to
      Spanish, except for incentive blurbs (which come from the API).
    
      Hit Calculate again and make sure the incentive blurbs are now in Spanish.
    oyamauchi committed Feb 9, 2024
    Configuration menu
    Copy the full SHA
    6043ad0 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    89527fa View commit details
    Browse the repository at this point in the history
  3. fix lint

    oyamauchi committed Feb 9, 2024
    Configuration menu
    Copy the full SHA
    5f3dced View commit details
    Browse the repository at this point in the history