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

Prettier formatted code with line break not recognized by i18n-ally #1011

Closed
jonathanwilke opened this issue Sep 8, 2023 · 7 comments · Fixed by #1014
Closed

Prettier formatted code with line break not recognized by i18n-ally #1011

jonathanwilke opened this issue Sep 8, 2023 · 7 comments · Fixed by #1014
Labels
bug Something isn't working

Comments

@jonathanwilke
Copy link

jonathanwilke commented Sep 8, 2023

When I have a translation key in my t() method that is very long, prettier breaks it into multiple lines and i18n-ally doesn't recognize this. I am using next-intl by @amannn
Bildschirmfoto 2023-09-07 um 17 30 35

@jonathanwilke jonathanwilke added the bug Something isn't working label Sep 8, 2023
@amannn
Copy link
Contributor

amannn commented Sep 8, 2023

Must be one of these regexes:

On a related note, I noticed that getTranslator(locale, namespace) should be considered equally to useTranslations when validating namespaces. That should be fixable with a regex adjustment too.

@ZerdoX-x
Copy link
Contributor

Same as #132 but for next-intl. Opened PR for this

@amannn
Copy link
Contributor

amannn commented Sep 12, 2023

Thanks a lot @ZerdoX-x! 🙌

Would you mind addressing the topic with getTranslator mentioned in my comment above too in your PR while you’re at it?

@ZerdoX-x
Copy link
Contributor

ZerdoX-x commented Sep 12, 2023

I noticed that getTranslator(locale, namespace) should be considered equally to useTranslations when validating namespaces.

Sorry, but I could not understand what exactly do you mean. Maybe due to lack of English language knowledge, maybe because I have never used next-intl.

I can't find getTranslator mention in next-intl.ts. Do you mean that getTranslator is the alias for useTranslations? Anyways looks like I am too unfamiliar with this, so I think it's better to provide another, separate issue and/or PR for this.

@amannn
Copy link
Contributor

amannn commented Sep 13, 2023

next-intl has an API like this:

function Component() {
  const t = useTranslations('Component');
  return <h1>{t('title')}</h1>;
}

useTranslations pre-configures the namespace and t resolves the remaining part. In this example the key Component.title is looked up when resolving the translation.

This scoping is implemented in getScopeRange:

https://github.com/lokalise/i18n-ally/blob/481859c845dd5f31717bc071bf174d6a584562bf/src/frameworks/next-intl.ts#L85C60-L85C60

Similarly, in the Metadata API of Next.js, this API can be used (docs):

import {getTranslator} from 'next-intl/server';
 
export async function generateMetadata({params: {locale}}) {
  const t = await getTranslator(locale, 'Metadata');
 
  return {
    title: t('title')
  };
}

Currently getTranslator isn't considered in getScopeRange though, therefore i18n-ally complains that the title key is missing.

The fix would be to adjust the regex mentioned above that looks for useTranslations. The tricky part might be that useTranslations takes the scope as the first argument, but getTranslator as the second.

But this can of course be handled in a separate issue, I thought I'll bring it up since it seems to be quite related! 🙂

@ZerdoX-x
Copy link
Contributor

@amannn thank you for explanation but I think I am not capable of this, sorry 😅
Let it be separate issue and PR

@amannn
Copy link
Contributor

amannn commented Sep 15, 2023

@ZerdoX-x Sure, I've opened #1017 for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

3 participants