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

Custom framework: Any way to match a namespace alongside the key for the usage match? #894

Open
baptisteArno opened this issue Mar 10, 2023 · 6 comments

Comments

@baptisteArno
Copy link
Contributor

baptisteArno commented Mar 10, 2023

Is your feature related to a specific framework or general for this extension
next-international

Is your feature request related to a problem? Please describe.
I'd like to be able to detect a namespace:

 const scopedT = useScopedI18n('folders')
[...]
scopedT('key')

It won't take folders namespace into consideration with this custom settings:

# .vscode/i18n-ally-custom-framework.yml

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

Describe the solution you'd like
Maybe adding {namespace} for the regex detection, that would add it as a prefix for the key? Or a namespaceMatchRegex config?

@amannn
Copy link
Contributor

amannn commented May 11, 2023

I'd be interested in this too! I guess this is required for #820 (if namespaces are used).

@amannn
Copy link
Contributor

amannn commented May 11, 2023

It seems like at least programmatically this should be possible:

// useTranslation
// https://react.i18next.com/latest/usetranslation-hook#loading-namespaces
getScopeRange(document: TextDocument): ScopeRange[] | undefined {
if (!this.languageIds.includes(document.languageId as any))
return
const ranges: ScopeRange[] = []
const text = document.getText()
// Add smaller local scope overrides first
// Namespaced prefixed keys already handled by rewriteKeys
// t('foo', { ns: 'ns1' })
const regT = /\Wt\([^)]*?ns:\s*['"`](\w+)['"`]/g
for (const match of text.matchAll(regT)) {
if (typeof match.index !== 'number')
continue
if (match[1]) {
ranges.push({
start: match.index,
end: match.index + match[0].length,
namespace: match[1],
})
}
}

@aranard
Copy link
Contributor

aranard commented Aug 30, 2023

This feature is added in #926, by including a new scopeRangeRegex property in the custom settings file.

@baptisteArno
Copy link
Contributor Author

This feature is added in #926, by including a new scopeRangeRegex property in the custom settings file.

That's nice! Unfortunately it won't work properly with that setup:

const t = useI18n()
const scopedT = useScopedI18n('scope')

// Use both t and scopedT

@aranard
Copy link
Contributor

aranard commented Sep 5, 2023

@baptisteArno ah I see - the issue is that you can only specify one regex, not multiple? That seems like it'd be straightforward to add. For my use case I only needed to set one, so didn't think to add the option for multiple.

@wesnolte
Copy link

wesnolte commented Jul 9, 2024

I attempted to work around this by using the regex "or" but it doesn't work for some reason. Any idea why? e.g.

scopeRangeRegex: "(use|get)ScopedI18n\\(\\s*\\[?\\s*['\"](.*?)['"]"

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

No branches or pull requests

4 participants