Skip to content

Investigate ways of generating _locales based on translation files #217

Closed as not planned

Description

@aklinker1

No, I meant... For someone who's not using vue-i18n and only relies on browser.i18n he'd be defining a mess in public/_locales because the syntax is too verbose.

But if someone wanted to use vue-i18n they'd be defining only extension name and description within the context of browser.i18n and the rest can all live inside vue-i18n locale dir.

Am I right? If not, I'd be trying to figure out the way to use only vue-i18n and somehow at the build-time generate locales needed for parts that browser.i18n can access, such as manifest.json file property values.

Originally posted by @dvlden in #187 (comment)

@dvlden Yes, that is an option, that could be a simple Vite plugin. I need to research other patterns before committing to adding that logic to WXT directly. I don't want to add something that only works for one framework's one library.

Originally posted by @aklinker1 in #187 (comment)

Goal here is to provide some kind of vite plugin or config option to generate the _locales directory from another localization file. This would also need to integrate with the type augmentation for browser.runtime.getMessage.

You shouldn't have to manage translation files in multiple directories.

Activity

  1. added this to the DX Improvements milestone on Nov 7, 2023
  2. changed the title [-]Investigate ways of generating `_locales` based on more standard translation files[/-] [+]Investigate ways of generating `_locales` based on translation files[/+] on Nov 7, 2023
  3. mefengl commented on Jan 3, 2024

    @mefengl
    Contributor

    I used an extension for VSCode called i18n-ally. I found it helpful, so I'm sharing it here in the hope that it might inspire solutions to this issue.

    https://github.com/lokalise/i18n-ally

  4. aklinker1 commented on Jan 3, 2024

    @aklinker1
    MemberAuthor

    Interesting tool, I hadn't heard about it before. I don't think it is the solution to combining manifest translations and other translation files though...

    https://github.com/lokalise/i18n-ally/wiki/Custom-Framework seems relevant, at least for adding editor support.

  5. mefengl commented on Jan 4, 2024

    @mefengl
    Contributor

    Yes, supporting this tool will greatly simplify the checking and editing of i18n files (it likely already support Chrome i18n and vue-i18n).


    Also, I have reviewed the i18n and vue-i18n wxt-examples and have gained a basic understanding of the situation. The current official structure, as per Google Chrome's standards, is:

    public/_locales/en/messages.json
    public/_locales/ko/messages.json
    

    For vue-i18n, WXT needs an official solution for the manifest, and a *.json solution for other files:

    assets/locales/en.json
    assets/locales/ko.json
    

    The objective is to utilize assets/locales/*.json for all i18n tasks (including manifest) across all frameworks, aiming to make this a built-in feature for WXT, possibly supporting the Google Chrome official solution simultaneously.

    Additionally, WXT requires a feature similar to useI18n to ensure type safety by verifying message keys exist.

    Is my understanding correct?

  6. aklinker1 commented on Jan 4, 2024

    @aklinker1
    MemberAuthor

    For the most part. Type-safety is a nice-to-have, not 100% necessary.

    It also needs to support react, Svelte, and any other i18n libraries. This is why I mentioned a custom vite plugin rather than something built into WXT - supporting every i18n library in core would not be a good approach.

    Creating separate plugins for each library could be a solution.

  7. mefengl commented on Jan 4, 2024

    @mefengl
    Contributor

    If type safety is not a concern, then actually, a plugin might not be necessary.
    Because the process of converting from assets/locales/<lang>.json to public/_locales/<lang>/messages.json is independent of the framework.

  8. aklinker1 commented on Jan 4, 2024

    @aklinker1
    MemberAuthor

    True, but features like placeholders, re-using strings, currency, and pluralization are different between most libraries.

    So just reading JSON from one place won't always work.

  9. mefengl commented on Jan 5, 2024

    @mefengl
    Contributor

    You shouldn't have to manage translation files in multiple directories.

    Initially, it was a bit confusing because I thought solving the problem mentioned above merely required a pure JavaScript implementation that should be independent of any framework, as it only needs to move the file from assets/locales/<lang>.json to public/_locales/<lang>/messages.json.


    features like placeholders, re-using strings, currency, and pluralization are different between most libraries

    This might not always hold true. For example, i18next is somewhat framework-independent:

    i18next.init({
      lng: 'en',
      resources: {
        en: {
          translation: {
            itemCount: "{{count}} item",
            itemCount_plural: "{{count}} items"
          }
        }
      }
    });
    
    i18next.t('itemCount', { count: 1 }); // 1 item
    i18next.t('itemCount', { count: 5 }); // 5 items
    

    But I understand that you're referring to a more framework-native approach to usage:

    import { FormattedNumber } from 'react-intl';
    
    <FormattedNumber value={20} style="currency" currency="USD" />
    
  10. nnx0r commented on Jan 5, 2024

    @nnx0r
    Contributor

    At the moment, I am opting out entirely of Vue i18n and using browser built-in i18n for all translation messages.

    It is disaster to maintain, but I wouldn't want to use i18n dependency inside of injection script or background / worker.

  11. aklinker1 commented on Jan 6, 2024

    @aklinker1
    MemberAuthor

    Ok, I'm convinced that WXT can't provide a solution for all localization frameworks, but it should be able to improve the existing i18n APIs and localization files.

    Here's what I propose:

    1. Create a new folder called locales/ recognized by WXT
    2. Place any files with translations directly in it (support YAML, JSON 5, and regular JSON)
    3. Improve the format of the localization files themselves (nested keys, optionally use a simple string instead of an object with a message, etc)

    I'll share a complete example of what I'm thinking in a bit.

  12. aklinker1 commented on Jan 6, 2024

    @aklinker1
    MemberAuthor

    For example, my GitHub Better Line Counts extension. It's not localized, but if I were to localize it, it would look something like this:

    src/
      locales/
        en.yml
        ...
    
    # src/locales/en.yml
    name: GitHub Better Line Counts
    description: Remove generated files from GitHub line counts
    diffs:
      additionsText:
        1: $count$ addition
        n: $count$ additions
      deletionsText: 
        1: $count$ deletion
        n: $count$ deletions
      generatedText: 
        1: $count$ generated line.
        n: $count$ generated lines.
      additionsSymbol: +$count$
      deletionsSymbol: −$count$
      generatedSymbol: ⌁$count$
    options:
      privateRepos:
        title: Access Private Repos
        description1: "Optional:"
        description2: To recalculate the diff on private repos, the extension needs a GitHub PAT to authenticate API requests.
        description3: Click here
        description4: to create one.
        invalidToken: Token is invalid
        validToken: Token is valid
        checking: Checking token...
        username: "Username: {username}"
      enabled:
        title: Show Generated Line Counts
        description1: Along with
        description2: +additions
        description3: and
        description4: −deletions
        description5: ", show a third number for"
        description6: ⌁generated
        description7: lines.
      customLists:
        title: Custom Lists
        description1: Use
        description1: minimatch
        description1: glob patterns to mark files as generated accross all repos.
        allRepos: All Repos
        customRepoPlaceholder: |
          Enter glob patterns:
          *.lock
          **/vendor/**

    Some comments:

    1. You don't need to use the { message: ..., description?: ..., placeholders?: ... } form the Chrome extensions require. If you just set a string value for a key, WXT will convert to it the form chrome extensions expect. You can still use the longer form if you want.
    2. Chrome doesn't have a built-in solution for pluralization. They recommend using "plural neutral forms" (Additions: $count$ instead of $count$ additions), which IMO doesn't always work. I plan on supporting plural forms, see diffs.*Text for an example.
  13. aklinker1 commented on Jan 6, 2024

    @aklinker1
    MemberAuthor

    Closing this issue, will continue conversation on #327 if you have comments or suggestions about the API I've decided on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions