Comments (10)
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:
useI18n
only returns a non-scopedt
function, we introduce a newuseScopedI18n
hook that takes the scope in parameters, similar to the currentscopedT
:
const t = useI18n() // non-scoped t
const t = useScopedI18n("scope") // scoped t
useI18n
can return both non-scoped or scopedt
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.
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.
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 tIf 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:
useI18n
only returns a non-scopedt
function, we introduce a newuseScopedI18n
hook that takes the scope in parameters, similar to the currentscopedT
:const t = useI18n() // non-scoped t const t = useScopedI18n("scope") // scoped t
useI18n
can return both non-scoped or scopedt
functions, based on if we add a scope in the parameters.const t = useI18n() // non-scoped t const t = useI18n("scope") // scoped tThe 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.
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.
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.
Oh, it would be a breaking change though
from next-international.
Can we release this? Or do we have anything else to add before?
from next-international.
I would like to release this along with Next.13 app directory support. Is the new scoped feature urgent for you?
from next-international.
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.
Closing this issue as this feature has been released in 0.4.0
from next-international.
Related Issues (18)
- Lot of potential but too strongly bounded by nextjs. HOT 6
- Nested object scopes HOT 6
- Usage with SSR (getServerSideProps) instead of SSG? HOT 3
- [RFC]: CLI Tool that transforms the language TS files to JSON and vice versa HOT 5
- error - SerializableError: Error serializing `.locale["pages.past.date"]` returned from `getServerSideProps` in "/". HOT 10
- Example usage with tests. HOT 5
- Object spreading locales breaks variable inference HOT 2
- Error: Uncaught [Error: `useI18n` must be used inside `I18nProvider`] HOT 8
- i18n support is not compatible with next export HOT 5
- Package incompatible with Next.JS 13 `/app` directory HOT 7
- pass t useI18n hook inside a another component HOT 2
- [BUG] Interpolation with variables does not work HOT 4
- Get current locale HOT 2
- Force consistency between all locale files HOT 2
- No matching version found for international-types@* inside the workspace HOT 3
- SSR Not working HOT 2
- Export `Locale` types to create custom `Locale` interface HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-international.