Comments (7)
Can we have these two features:
- If the translation does not exist, fallback to see the defaultLocale json file and use the word there
- Fallback to a specific language not always defaultLocale, If the translation from
zh-TW
does not exist, fallback to show words inzh
from next-intl.
Can we have these two features:
- If the translation does not exist, fallback to see the defaultLocale json file and use the word there
- Fallback to a specific language not always defaultLocale, If the translation from
zh-TW
does not exist, fallback to show words inzh
I use this code to implement the logic:
import merge from "deepmerge";
import { getRequestConfig } from "next-intl/server";
export default getRequestConfig(async ({ locale }) => {
const localeMessages = (await import(`./messages/${locale}.json`)).default;
const localeGeneralMessages =
locale.split("-").length > 1
? (await import(`./messages/${locale.split("-")[0]}.json`)).default
: {};
const defaultMessages = (await import(`./messages/en.json`)).default;
const messages = merge.all([
defaultMessages,
localeGeneralMessages,
localeMessages,
]);
return {
messages,
};
});
It can run successfully, but give me the type warning for async ({ locale })
Argument of type '({ locale }: GetRequestConfigParams) => Promise<{ messages: object; }>' is not assignable to parameter of type '(params: GetRequestConfigParams) => RequestConfig | Promise<RequestConfig>'.
Type 'Promise<{ messages: object; }>' is not assignable to type 'RequestConfig | Promise<RequestConfig>'.
Type 'Promise<{ messages: object; }>' is not assignable to type 'Promise<RequestConfig>'.
Type '{ messages: object; }' is not assignable to type 'RequestConfig'.
Types of property 'messages' are incompatible.
Type 'object' is not assignable to type 'AbstractIntlMessages'.
Index signature for type 'string' is missing in type '{}'.ts(2345)
anyone know how to solve this?
from next-intl.
Merging messages from another locale as a fallback is absolutely fine and also mentioned in the messages docs ("How can I use messages from another locale as fallbacks?").
from next-intl.
Can we have these two features:
- If the translation does not exist, fallback to see the defaultLocale json file and use the word there
- Fallback to a specific language not always defaultLocale, If the translation from
zh-TW
does not exist, fallback to show words inzh
I use this code to implement the logic:
import merge from "deepmerge"; import { getRequestConfig } from "next-intl/server"; export default getRequestConfig(async ({ locale }) => { const localeMessages = (await import(`./messages/${locale}.json`)).default; const localeGeneralMessages = locale.split("-").length > 1 ? (await import(`./messages/${locale.split("-")[0]}.json`)).default : {}; const defaultMessages = (await import(`./messages/en.json`)).default; const messages = merge.all([ defaultMessages, localeGeneralMessages, localeMessages, ]); return { messages, }; });It can run successfully, but give me the type warning for
async ({ locale })
Argument of type '({ locale }: GetRequestConfigParams) => Promise<{ messages: object; }>' is not assignable to parameter of type '(params: GetRequestConfigParams) => RequestConfig | Promise<RequestConfig>'. Type 'Promise<{ messages: object; }>' is not assignable to type 'RequestConfig | Promise<RequestConfig>'. Type 'Promise<{ messages: object; }>' is not assignable to type 'Promise<RequestConfig>'. Type '{ messages: object; }' is not assignable to type 'RequestConfig'. Types of property 'messages' are incompatible. Type 'object' is not assignable to type 'AbstractIntlMessages'. Index signature for type 'string' is missing in type '{}'.ts(2345)
anyone know how to solve this?
I've solved the issue like this, but I only have two elements, maybe you can do two steps
const messages = merge(defaultMessages, localeMessages);
from next-intl.
My usecase:
Our backend is returning a State
(e.g. Washington, Oregon, etc.)
We will have translations for all of the common states.
However, it is possible that a new value comes down the line.
I would like to be able to show a translated key if its present, otherwise just fall back to displaying the value returned by the API.
As far as I can tell, this isn't supported by this library?
from next-intl.
@philipbjorge Have you considered using select for this use case?
"state": "{state, select, WA {Washington} OR {Oregon} other {{state}}"
from next-intl.
Mmm interesting solution... We have 900 selections, so I feel like that string might become unwieldy. I think we might go with something like this since the key name is well-defined.
const getTranslationWithFallback = (key: string, fallback: string) => {
const translated = tc(key, false);
if (translated.startsWith("common.")) {
return fallback;
}
return translated;
};
{getTranslationWithFallback(`biomes.${biome}`, biome)}
from next-intl.
Related Issues (20)
- Error "page not found" in the middleware when accessing the API HOT 1
- numbers are translated when using .rich HOT 1
- useLocale() disables fetch tags cache ( next tags ) HOT 1
- Is it possible to read the value of another namespace in an existing namespace? HOT 1
- Locale Detection: Option to change order HOT 1
- [Docs]: Middleware documentation is too light HOT 1
- Unable to use with RSC when mode is async HOT 3
- Localized navigation doesn't work with too many pathnames (over 25) HOT 2
- false NEXT_LOCALE cookie value when entring route directly HOT 3
- Issue with multiple middleware chain HOT 1
- ESLint plugin HOT 2
- Handling Unsupported Languages HOT 1
- Support `localePrefix` setting per domain HOT 2
- Support Intl.RelativeTimeFormat for `relativeTime` formatting HOT 3
- Docs Invisible Text on Search HOT 1
- Package path ./server.js is not exported from package HOT 2
- Error using `as={Link}` prop with when using `createSharedPathnamesNavigation` HOT 1
- Accidentally changed the locale after navigating when setting "localePrefix" to "never" HOT 2
- I cannot understand how to merge next-intl middleware with nextjs/platform HOT 3
- unhandledRejection: Error: NEXT_NOT_FOUND on terminal after running npm run dev HOT 10
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-intl.