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 309 forks source link

next-translate: useTranslation not working with default namespace #752

Open thlcodes opened 2 years ago

thlcodes commented 2 years ago

Hi folks šŸ‘‹!

First of all: great tool! Keep up the good work!

When I try to use i18n Ally in combination with next-translate and use useTranslation with a default namespace (e.g. "foo"), I get

i18n key "bar" does not exist(i18n-ally-key-missing)

As far as I get that *should work, right?

Cheers, Thomas

Extension Version i18n Ally v2.8.1

Framework/i18n package you are using next-translate 1.4.0

To Reproduce Steps to reproduce the behavior:

  1. See screenshot

Device Infomation

Extension Log Go to View -> Output -> i18n Ally, and paste the content below. You should mask any sensitive information

šŸˆ¶ Activated, v2.8.1

ā€•ā€•ā€•ā€•ā€•ā€•

šŸ’¼ Workspace root changed to "/workspaces/i18n-test/i18n-test"
šŸ“¦ Packages file "package.json" found
šŸ•³ Packages file "pubspec.yaml" not exists
šŸ•³ Packages file "composer.json" not exists
šŸ•³ Packages file "Gemfile" not exists
šŸŒž Enabled
šŸ§© Enabled frameworks: Next Translate, General
šŸ§¬ Enabled parsers: json, yaml, json5

šŸ“ˆ Telemetry id: 31a87a18-4359-444f-9d4d-029a66a7960b
šŸš€ Initializing loader "/workspaces/i18n-test/i18n-test"
šŸ“‚ Directory structure: dir
šŸ—ƒ Path Matcher Regex: /^(?<locale>[\w-_]+)\/(?<namespace>[^/\\]+)\.json$/,/^(?<locale>[\w-_]+)(?:.*\/|^)(?<namespace>[^/\\]+)\.(?<ext>json|ya?ml|json5)$/

šŸ“‚ Loading locales under /workspaces/i18n-test/i18n-test/texts
    šŸ“‘ Loading (de) de/common.json [1649848354895.543]
    šŸ“‘ Loading (de) de/foo.json [1649946235213.564]
    šŸ“‘ Loading (en) en/common.json [1649848334702.393]
    šŸ“‘ Loading (en) en/foo.json [1649946276824.8071]

šŸ‘€ Watching change on /workspaces/i18n-test/i18n-test/texts
āœ… Loading finished

Screenshots

Screenshot 2022-04-14 at 16 50 26

EDIT: what of cause is hidden in the screenshot (by the error message):

const {t, lang} = useTranslation("foo");
Arnaud-J commented 2 years ago

I am having the same problem when relying on default namespace with react and i18next frameworks: šŸ§© Enabled frameworks: i18next, React, General.

As a workaround, the namespace should be specified explicitly when translating.

@thlcodes You spelled userTranslation in the issue title. Could you rename to useTranslation so that your issue is easier to find?

LaCocoRoco commented 2 years ago

@thlcodes @Arnaud-J Thanks for showing the namespace workaround. I switched from next-i18next to next-translate and couldn't find a fix for the i18n-ally extension problem.

cassus commented 1 year ago

Seems to me that a similar issue has been fixed in src/frameworks/i18next.ts using getScopeRange

AdamQuadmon commented 1 year ago

in addition to @Arnaud-J suggestion for explicit namespace declaration I had to specify also the path to my locales in i18n-ally.localesPaths settings and manually select only json as "Enabled Parser" as suggested in this other issue on the plugin repo https://github.com/lokalise/i18n-ally/issues/816

so instead of

  const { t } = useTranslation('dashboard');

I have to explicitly declare the full namespace in the function

t("dashboard.errors.required")

it will be nice to leverage on default namespace

wesnolte commented 10 months ago

I used https://github.com/lokalise/i18n-ally/issues/307 as a guide and have created this feature for next-translate. I don't know TS and have never worked on a VSCode extension before so not sure if there are issues. I've done some testing and it works as I expect it to. Please take a look at my fork and let me know if there are any glaring issues. If not I'll issue a pull request.

https://github.com/lokalise/i18n-ally/commit/6ed5ad49427f09247752ad94dae07658fffb4f8d

Chealer commented 2 weeks ago

This seems to affect react-i18next too.