Giter VIP home page Giter VIP logo

Comments (10)

QuiiBz avatar QuiiBz commented on September 4, 2024 1

I agree that this pattern const { scopedT } = ... const t = ... often repeats (e.g in every pages on this project: https://github.com/lagonapp/lagon/blob/02fe4e40e487c851fbf0fa6dfbd866c3726f0b76/packages/website/pages/index.tsx#L17)

The current API looks like this:

const { t, scopedT } = useI18n() // non-scoped t
const t = scopedT('scope') // scoped t

If you only want to have a scoped t, you currently have to write two lines, and that can be shorten. We have multiple solutions for this:

  1. useI18n only returns a non-scoped t function, we introduce a new useScopedI18n hook that takes the scope in parameters, similar to the current scopedT:
const t = useI18n() // non-scoped t
const t = useScopedI18n("scope") // scoped t
  1. useI18n can return both non-scoped or scoped t functions, based on if we add a scope in the parameters.
const t = useI18n() // non-scoped t
const t = useI18n("scope") // scoped t

The second solution is "cleaner" but might be harder to type correctly. What do you think?

from next-international.

QuiiBz avatar QuiiBz commented on September 4, 2024 1

Either solution would be a breaking change, but I don't think it's bad because it improves the DX while preparing for the v1. Feel free to submit a PR!

from next-international.

cstrnt avatar cstrnt commented on September 4, 2024

I agree that this pattern const { scopedT } = ... const t = ... often repeats (e.g in every pages on this project: lagonapp/lagon@02fe4e4/packages/website/pages/index.tsx#L17)

The current API looks like this:

const { t, scopedT } = useI18n() // non-scoped t
const t = scopedT('scope') // scoped t

If you only want to have a scoped t, you currently have to write two lines, and that can be shorten. We have multiple solutions for this:

  1. useI18n only returns a non-scoped t function, we introduce a new useScopedI18n hook that takes the scope in parameters, similar to the current scopedT:
const t = useI18n() // non-scoped t
const t = useScopedI18n("scope") // scoped t
  1. useI18n can return both non-scoped or scoped t functions, based on if we add a scope in the parameters.
const t = useI18n() // non-scoped t
const t = useI18n("scope") // scoped t

The second solution is "cleaner" but might be harder to type correctly. What do you think?

I actually like both solutions. I think the second one should be easy to type with a function overload tbh. But It might be confusing to some people that adding a param changes the return type of a function. Maybe we could do both?

from next-international.

QuiiBz avatar QuiiBz commented on September 4, 2024

But It might be confusing to some people that adding a param changes the return type of a function.

Strongly agree with this, but still not sure what the best is for the DX:
The first one is more explicit - you know that it's a scoped translation, but you have to import a different hook.
The second one can be a bit "magical" as the type change if we add a scope or not, which might not be obvious at first, but is easier to use since it's the same hook.

from next-international.

baptisteArno avatar baptisteArno commented on September 4, 2024

Also, if you need both t and scopedT in a component, you can't write:

const { t, scopedT } = useI18n()
const t = scopedT('scope')

Because t is already declared.

So, the first suggestion is 🔥

Can I create a PR for this?

from next-international.

baptisteArno avatar baptisteArno commented on September 4, 2024

Oh, it would be a breaking change though

from next-international.

baptisteArno avatar baptisteArno commented on September 4, 2024

Can we release this? Or do we have anything else to add before?

from next-international.

QuiiBz avatar QuiiBz commented on September 4, 2024

I would like to release this along with Next.13 app directory support. Is the new scoped feature urgent for you?

from next-international.

baptisteArno avatar baptisteArno commented on September 4, 2024

I'm just implementing next-international in my project right now and would like to start off on a good basis. But I'll fork the project for now, that's okay! 👍

from next-international.

QuiiBz avatar QuiiBz commented on September 4, 2024

Closing this issue as this feature has been released in 0.4.0

from next-international.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.