Git Product home page Git Product logo

Comments (7)

Crayon-ShinChan avatar Crayon-ShinChan commented on June 27, 2024

Can we have these two features:

  1. If the translation does not exist, fallback to see the defaultLocale json file and use the word there
  2. Fallback to a specific language not always defaultLocale, If the translation from zh-TW does not exist, fallback to show words in zh

from next-intl.

Crayon-ShinChan avatar Crayon-ShinChan commented on June 27, 2024

Can we have these two features:

  1. If the translation does not exist, fallback to see the defaultLocale json file and use the word there
  2. Fallback to a specific language not always defaultLocale, If the translation from zh-TW does not exist, fallback to show words in zh

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.

amannn avatar amannn commented on June 27, 2024

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.

alejandroTecDam avatar alejandroTecDam commented on June 27, 2024

Can we have these two features:

  1. If the translation does not exist, fallback to see the defaultLocale json file and use the word there
  2. Fallback to a specific language not always defaultLocale, If the translation from zh-TW does not exist, fallback to show words in zh

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.

philipbjorge avatar philipbjorge commented on June 27, 2024

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.

amannn avatar amannn commented on June 27, 2024

@philipbjorge Have you considered using select for this use case?

"state": "{state, select, WA {Washington} OR {Oregon} other {{state}}"

from next-intl.

philipbjorge avatar philipbjorge commented on June 27, 2024

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)

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.