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

feat: Improved localization API #343

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

Conversation

aklinker1
Copy link
Collaborator

@aklinker1 aklinker1 commented Jan 13, 2024

This closes #327 . It adds a new module (that's auto-imported by default), wxt/i18n. To use the API:

# locales/en.yml
hello: Hello $1
items:
  1: 1 item
  n: $1 items
import { i18n } from 'wxt/i18n';

i18n.t("hello", ["Mark"]); // "Hello Mark"
i18n.tp("items", 2, ["1"]) // "2 items"
i18n.tp("items", 1, ["1"]) // "1 item"
  • Implement conversion tools for mapping message files
  • Implement client code
    • Plural support
      • Double check if plural logic works as expected for definitions without 0 or 1, just n
    • Substitutions
    • Overridable types
    • Non-overridden types work
    • Typed placeholders Will add this in a future PR, but the overidable types support defining types for the placeholders.
  • Update type generation for browser.i18n.getMessage
  • Generate types for new i18n variable
  • Update tests
  • Update docs
  • Release test version
  • Create PR to update i18n examples
  • Check compatibility with i18next VS code plugin

Copy link

netlify bot commented Jan 13, 2024

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 98bc1f2
🔍 Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/65a460e85fd1b30008c867c5
😎 Deploy Preview https://deploy-preview-343--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@aklinker1 aklinker1 mentioned this pull request Jan 13, 2024
8 tasks
Copy link

codecov bot commented Jan 14, 2024

Codecov Report

Attention: 25 lines in your changes are missing coverage. Please review.

Comparison is base (a329e24) 78.39% compared to head (98bc1f2) 78.69%.
Report is 1 commits behind head on main.

Files Patch % Lines
src/i18n/index.ts 0.00% 20 Missing and 1 partial ⚠️
src/i18n/node.ts 98.57% 3 Missing ⚠️
src/types/internal.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #343      +/-   ##
==========================================
+ Coverage   78.39%   78.69%   +0.30%     
==========================================
  Files         102      106       +4     
  Lines        7128     7469     +341     
  Branches      622      663      +41     
==========================================
+ Hits         5588     5878     +290     
- Misses       1523     1572      +49     
- Partials       17       19       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aklinker1 aklinker1 marked this pull request as ready for review January 14, 2024 22:30
package.json Outdated
@@ -1,7 +1,7 @@
{
"name": "wxt",
"type": "module",
"version": "0.14.1",
"version": "0.14.2-alpha1",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert before merging

@aklinker1
Copy link
Collaborator Author

@dvlden I've released v0.14.2-alpha2 for testing:

pnpm i wxt@next

@dvlden
Copy link
Contributor

dvlden commented Jan 14, 2024

Amazing! I will definitely test it tomorrow. It's late now and I'm headed to bed. Giving you my feedback tomorrow, thanks man! 🤙

@aklinker1
Copy link
Collaborator Author

aklinker1 commented Jan 14, 2024

Alright, I've integrated the alpha release into one of my extensions to try it out. Everything works great at runtime, but there's some DX improvements I'd like to make:

  1. I shouldn't have to cast a number to a string when passing it as a substitution:
    -i18n.tp("diffs_additionsText", count, [String(count)])
    +i18n.tp("diffs_additionsText", count, [count])
  2. I don't like having plurals as a separate function. tpt.
    -i18n.tp("diffs_additionsText", count, [count])
    +i18n.t("diffs_additionsText", count, [count])
  3. For single substitutions, I shouldn't have to use an array.
    -i18n.tp("diffs_additionsText", count, [count])
    +i18n.t("diffs_additionsText", count, count)
  4. The nested messages are hard to edit. I can't double click to select one "level" of the message name, like "description1" to quickly change it to "description2", because _ are not considered word breaks. Same with using ctrl+backspace or opt+delete to backspace an entire word.
    <p class="font-medium text-base-content">
      {{ t("options_customLists_description1") }}
    </p>
    Ideally, we would use periods to denote nested levels, like options.customLists.description1, but . is not a valid character for a message name in web extensions... So we would have to write them to the disk with _, and replace all . with underscores when grabbing the keys at runtime... I don't want to cause a performance issue when running .replaceAll(".", "_"), but it might be worth it for the convenience...
  5. Log a warning and fail the build when a locales directory is present, but no default_locale is specified in the manifest, and vise-versa. If we kept the build going, Chrome would fail to load both these extensions, so it's better we catch this sooner.
  6. Unlike the browser.runtime.getMessage API, I can't hover over the function to see the description and text. It's an easy, built-in way of quickly viewing what the translation text is.
    Screenshot 2024-01-14 at 5 24 12 PM
    Screenshot 2024-01-14 at 5 23 35 PM
  7. Substitutions array is typed as any, it should be string[]

@dvlden
Copy link
Contributor

dvlden commented Jan 15, 2024

Sorry for slower response. I just grabbed a chance to test this.

Notes (alpha):

  • Running without locales dir
Failed to update .wxt directory: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined
  • Looks like we are not utilising arrays in locales. Assuming that browser.i18n does not support it natively (did not research)
# Not working
state:
  - Normal
  - Upscale
  - Stretch
  • Does not seem to support multiple nesting levels. Not sure why, cause in your example it looks like it does?
activity:
  # ...
  state:
    active: On
    inactive: Off
// Shows autocompletion, but renders as empty string
i18n.t('activity_state_active')
  • Does not trigger hot-reload if locales/xx.yaml is changed (new properties or editing existing ones)

I think I found it most annoying, not being able to click on the property key to edit the locale message. You said it doesn't let you do it for nested properties, but I can't do it with a non-nested property either. Not sure if I need an extension for that, but native browser.i18n.getMessage('xxx') is also not editable through VSCode with CMD + Click on the argument.

@aklinker1
Copy link
Collaborator Author

Good feedback, missed all that. I'll try and get an improved version released next weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improved localization APIs
2 participants