Skip to content

introduce returnResolved t option that pratically exposes the result of the resolve function #1764

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

Merged
merged 11 commits into from
May 6, 2022

Conversation

adrai
Copy link
Member

@adrai adrai commented May 5, 2022

usage:

i18next.t('key', { returnResolved: true })
// returns {
//   usedKey: 'key',
//   res: 'translation value,
//   exactUsedKey: 'key',
//   usedLng: 'en',
//   usedNS: 'translation',
// }

While this PR technically works to help in use cases like #1763 I'm not sure if we should offer this kind of interface...

Normally the lang HTML attribute is set to the i18next.resolvedLanguage value.

Checklist

  • only relevant code is changed (make a diff before you submit the PR)
  • run tests npm run test
  • tests are included

@coveralls
Copy link

coveralls commented May 5, 2022

Coverage Status

Coverage decreased (-0.3%) to 90.698% when pulling c9754be on adrai:master into 7475e9a on i18next:master.

@adrai
Copy link
Member Author

adrai commented May 5, 2022

@pedrodurek ok for the types? Or is there a clash when using also returnObjects: true ?

@pedrodurek
Copy link
Member

hey @adrai, it's good enough 😄 , nice job!

@adrai
Copy link
Member Author

adrai commented May 5, 2022

@pedrodurek can't we change the return type based on the returnResolved option?

Something like:

/**
 * Object returned from t() function when passed returnResolved: true option.
 */
export type ResolvedResult = {
  usedKey: string;
  res: TFunctionResult;
  exactUsedKey: string,
  usedLng: string,
  usedNS: string,
};
export type TFunctionResult = string | object | ResolvedResult | Array<string | object> | undefined | null;
export type TFunctionKeys = string | TemplateStringsArray;
export interface TFunction {
  // basic usage
  <
    TReturnResolved extends boolean | undefined,
    TResult extends TFunctionResult = TReturnResolved extends true ? ResolvedResult : string,
    TKeys extends TFunctionKeys = string,
    TInterpolationMap extends object = StringMap & { returnResolved?: TReturnResolved },
  >(
    key: TKeys | TKeys[],
    options?: TOptions<TInterpolationMap> | string,
  ): TResult;

This will for sure not work, because I've no clue of TypeScript, but just to give you the idea of what I'm asking for...

basically if returnResolved is passed with true, the ResolvedResult type should be returned, else the string type...

@adrai
Copy link
Member Author

adrai commented May 6, 2022

I think I got it: cf5bc34

@adrai
Copy link
Member Author

adrai commented May 6, 2022

@pedrodurek I've also tried with the returnObjects case: 5eaec77

Can you check if this is ok or if I messed up everything?

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

Thank you for your proposal! Looks great!

  1. I wonder whether it could be of interest for someone to configure returnResolved in the i18next options.
  2. returnResolved – it is not clear what "resolved" means. It would be the first occurrence of this term in the API with that meaning. Maybe name it returnDetails?

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

Is it already possible to configure returnResolved: true in the i18next global options? That would probably break the current TypeScript typing.

@adrai
Copy link
Member Author

adrai commented May 6, 2022

Is it already possible to configure returnResolved: true in the i18next global options? That would probably break the current TypeScript typing.

no, this is not a global option yet... I don't think this makes sense to make this a global option, right?

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

Is it already possible to configure returnResolved: true in the i18next global options? That would probably break the current TypeScript typing.

no, this is not a global option yet... I don't think this makes sense to make this a global option, right?

I'm a bit inclined to say that it could make sense, but I'm probably biased.

@adrai
Copy link
Member Author

adrai commented May 6, 2022

Is it already possible to configure returnResolved: true in the i18next global options? That would probably break the current TypeScript typing.

no, this is not a global option yet... I don't think this makes sense to make this a global option, right?

I'm a bit inclined to say that it could make sense, but I'm probably biased.

ok, now it is... for JS there is for sure no problem... but I have no idea about TS

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

returnResolved – it is not clear what "resolved" means. It would be the first occurrence of this term in the API with that meaning. Maybe name it returnDetails?

verbose is also not bad for someone who is familar with UNIX CLI tools.

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

ok, now it is... for JS there is for sure no problem... but I have no idea about TS

thank you

@adrai
Copy link
Member Author

adrai commented May 6, 2022

regarding the name: I'm not bound to returnResolved, I just took that, because internally the translation key needs to be resolved and the resulting information is exactly what we return...
But I can rename this to verbose or returnDetails or returnMeta or returnInfo or whatever you like....

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

verbose is also not bad for someone who is familar with UNIX CLI tools.

Ok, after thinking a bit about "verbose", that could be a bit confusing considering that this is a localization/translation library. 🤔

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

I think my favorite name is returnDetails, which is exactly what this option does. returnResolved does not imply that t would return more details; when t returns only the translation, the translation could also be seen as something which was "resolved", so for someone else it could be unclear what returnResolved: false does.

@adrai
Copy link
Member Author

adrai commented May 6, 2022

renamed

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

renamed

😍

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

thank you, I like it

@adrai
Copy link
Member Author

adrai commented May 6, 2022

Do you think it is ready to be merged?

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

Do you think it is ready to be merged?

Yes. TFunctionDetailedResult is undocumented, but the documentation resides in another repository, right?

@adrai
Copy link
Member Author

adrai commented May 6, 2022

Yes. TFunctionDetailedResult is undocumented, but the documentation resides in another repository, right?

Do you mean this? c9754be

@c7hm4r
Copy link

c7hm4r commented May 6, 2022

Yes. TFunctionDetailedResult is undocumented, but the documentation resides in another repository, right?

Do you mean this? c9754be

Perfect

@adrai adrai merged commit 6b2e42e into i18next:master May 6, 2022
@c7hm4r
Copy link

c7hm4r commented May 6, 2022

Thank you very much again!

@adrai
Copy link
Member Author

adrai commented May 6, 2022

released with v21.7.0

@adrai
Copy link
Member Author

adrai commented May 6, 2022

If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project.

There are many ways to help this project 🙏

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

Successfully merging this pull request may close these issues.

4 participants