Comments (7)
Also, this would remove the need for some methods in createI18n
that were used only to get the right type, resulting in better code-splitting.
from next-international.
I understand better the issue. We can definitely export those two types and add an example in the README on how to set up an explicit interface for locales.
from next-international.
Landed in version 0.0.5 (#10), thanks!
from next-international.
I like this idea. It can be taken even further by overriding an interface declared in next-international
, to avoid the generic on createI18n
, and import defineLocale
directly from the package:
// locales/index.ts
import { createI18n } from 'next-international';
declare module 'next-international' {
interface Locale {
hello: string
}
}
export const {
useI18n,
// ...
} = createI18n({
en: () => import('./en'),
});
// locales/en.ts
import { defineLocale } from 'next-international';
export default defineLocale({
hello: 'Hello',
})
The downside is that you must duplicate all your keys to define them in the type (MyCustomLocale
in your example), instead of writing them only in the locale files.
But it should also work with JSON locales, so maybe we can explicitly type the parameters to get type-safety while using JSON files:
// locales/en.json
{
"hello": "Hello {param}, this is {anotherParam}"
}
// locales/index.ts
interface Locale {
hello: '{param} {anotherParam}';
}
const { useI18n } = createI18n({
en: () => import('./en.json'),
});
const { t } = useI18n()
// can have type-safety
t('hello', {
param: '',
anotherParam: '',
})
from next-international.
After trying a bit more moving in this direction, I'm not sure if it's a good idea:
- One of the goals of this lib is to avoid explicitly creating the types for the locales, which will not be the case if we move in this direction
- Adding a new key in locales should be as simple as adding it to the locales files - here, we would need to also add it to the interface in order to use it
Let me know what you think.
from next-international.
These are fair points. However I currently feel like offering the option to create an explicit locale interface would still be good.
The primary reason I am in favour of offering it as an option is this:
Adding a new key in locales should be as simple as adding it to the locales files
For example, if a new hire or community member wants to contribute to a OS project & they have not used next-international
before they may not understand that in order to change or update the locale schema they will need to update the "default locale", or whatever one is used for type inference, first in order for the type inference to be updated & other locales to require the new locales.
Some people may like the current method, and that is fine, but I personally find it odd to need to update the data structure of one file to change others & think it would be better to offer explicit types as an option.
Currently there is a simple way around this by just redefining the LocaleValue
& Locale
types:
type LocaleValue = string | number;
type BaseLocale = Record<string, LocaleValue>;
While this works it would be better if these were exported by the package to be consumed in case of any changes during dependency upgrades.
from next-international.
Created #10, feel free to check the PR and leave me your feedback.
from next-international.
Related Issues (20)
- Feedback for “Setup”
- Complex inference is making editors go haywire, @ts-nocheck does not resolve this. HOT 3
- Should I place my api routes under [locale] ? HOT 2
- Question: How to Insert Line Breaks Using Params in One Language Only HOT 2
- Saving and loading language preference from persistent storage
- Support for multiple scopes HOT 1
- [Feature] Direction Provider
- Default locale not added on redirect
- import_server is not defined
- How to enable SSR for pages dir? (pageProps.locale is undefined during SSR rendering) HOT 3
- Using Cloudflare Pages forgets the selected language HOT 2
- Is there any way to adopt i18n without sub-path routing on Pages Router?
- Question: Is there feature for use translate outside hook and component? HOT 2
- [SSG][Metadata] AlternateURLs/Languages ; OpenGraphMetadata
- Failed to execute 'removeChild' on 'Node' when using `useChangeLocale` and `useCurrentLocale`
- is there ignoreRoutes property for ignore adding localization route to static file HOT 1
- Feedback for “Setup” HOT 1
- [Question][Maybe due to the middleware] Forced `canonical` on OpenGraph? HOT 1
- Display Loading Indicator During Language Translation Process HOT 1
- Unit testing with createI18nServer in server-side components throws server-only error HOT 1
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.