lokalise / i18n-ally

🌍 All in one i18n extension for VS Code
https://marketplace.visualstudio.com/items?itemName=lokalise.i18n-ally
MIT License
3.91k stars 310 forks source link

[next-intl] Not parsing the namespace key correctly when using getTranslations #1170

Open ellisio opened 2 months ago

ellisio commented 2 months ago

Describe the bug When using getTranslations({ locale, namespace: "Metadata" }) inside of the generateMetadata() function, the parsing is incorrect. I would expect this to return only title, as that is the only item in that namespace in en.json.

Extension Version v2.12.0

Framework/i18n package you are using next-intl

To Reproduce

  1. Use the following .vscode/settings.json config:
    "i18n-ally.localesPaths": ["./apps/app/messages"],
    "i18n-ally.enabledFrameworks": ["next-intl"],
    "i18n-ally.namespace": true,
    "i18n-ally.keystyle": "nested",
    "i18n-ally.sourceLanguage": "en",
  2. Use a basic Turborepo project with NextJS.
  3. View screenshot below for bad output.

Device Infomation

Extension Log

🈢 Activated, v2.12.0

――――――

πŸ’Ό Workspace root changed to "/Users/aellis/Developer/project"
🌞 Enabled
🧩 Enabled frameworks: next-intl
🧬 Enabled parsers: json, yaml, json5

πŸ“ˆ Telemetry id: 29ea9984-1ad8-4195-8059-25bb0c667d35
πŸš€ Initializing loader "/Users/aellis/Developer/project"
πŸ“‚ Directory structure: file
πŸ—ƒ Path Matcher Regex: /^(?<locale>[\w-_]+)\.(?<ext>json|ya?ml|json5)$/

πŸ“‚ Loading locales under /Users/aellis/Developer/project/apps/app/messages
    πŸ“‘ Loading (en) en.json [1718916883954.7827]
    πŸ“‘ Loading (es) es.json [1718916458935.8823]

πŸ‘€ Watching change on /Users/aellis/Developer/project/apps/app/messages
βœ… Loading finished

Screenshots image image image

DarioLopes commented 1 month ago

The fix is on another post but here is the solution :

{
    "i18n-ally.enabledFrameworks": ["custom"], // * This is the fix for the getTranslations issue
    "i18n-ally.localesPaths": ["src/i18n/messages"], // Change it to match your folder with all the JSON messages
    "i18n-ally.sourceLanguage": "en", // Language that will replace the key on the file
    "i18n-ally.sortKeys": true // On key edit through the plugin, sort the JSON
}

note that only i18n-ally.enabledFrameworks is part of the fix, other settings are here just to show how it works / more options.

languageIds:
    - javascript
    - typescript
    - javascriptreact
    - typescriptreact

usageMatchRegex:
    - "[^\\w\\d]t\\(['\"`]({key})['\"`]"

scopeRangeRegex: "(?:useTranslations\\(|getTranslations\\(|namespace:)\\s*['\"`](.*?)['\"`]"

monopoly: true

The repo where the solution is can be found here and I found this thanks to @ixartz answer