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

add keyPath support for react-i18n-next #770

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vovsemenv
Copy link

Allow keyPath support for react-i18n-next useTranslation hook

closes #719

@vovsemenv
Copy link
Author

@terales can you check it?

@soapwong703
Copy link

soapwong703 commented Jun 2, 2022

螢幕截圖 2022-06-02 下午7 15 14

Thanks for the help, I defenitely need this feature! However I pulled your PR and are having two problems:

  1. keyPrefix will go before the namespace ('component.common.title')
  2. the first keyPrefix in the file will override the later keyPrefix ('component.common.submit')

The correct key should be 'common.component.title' and 'common.action.page'.

Would you kindly fix it?

@vovsemenv
Copy link
Author

@soapwong703
i can fix this part (in my setup i use only default namespace)

  1. keyPrefix will go before the namespace ('component.common.title')

in my realization extension can work only be configured in file scope so you shoud have only one component in one file, but you can help me to fix it

the first keyPrefix in the file will override the later keyPrefix ('component.common.submit')

@JulesLecuir
Copy link

Hi @vovsemenv how is this PR going ? Looking forward to it ! :)

@vovsemenv
Copy link
Author

i guess it waiting for @terales review


function detectedKey(key: string, documentText?: string): string {
if (!documentText) return key
const mathces = documentText.match(/useTranslation\(.+,\s*{\s*keyPrefix:\s*["'`](?<keyPath>.+)["'`]\s*}\s*\)/)

Choose a reason for hiding this comment

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

oops, little typo here ! ;)

const keyPath = mathces?.groups?.keyPath
if (!keyPath) return key

return `${keyPath}.${key}`

Choose a reason for hiding this comment

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

I guess the key separator should be dynamic, as it can be configured in the i18nally configuration

Copy link
Collaborator

@terales terales left a comment

Choose a reason for hiding this comment

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

Can we add a test here (at least as a small e2e test to the https://github.com/lokalise/i18n-ally/tree/main/examples/by-frameworks)?

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

Successfully merging this pull request may close these issues.

Support keyPrefix options from react-i18next
4 participants