Code Monkey home page Code Monkey logo

Comments (5)

sorrycc avatar sorrycc commented on August 20, 2024 1

补充下用户侧是怎么用的,

  1. 在 locales 目录下怎么写
  2. 组件层怎么引,比如 import { useLocale } from 'umi' 这样?
  3. 路由里怎么配
  4. 其他的场景

from umi-next.

chenshuai2144 avatar chenshuai2144 commented on August 20, 2024 1

动态增加新语言

 useEffect(() => {
    addLocale('zh-TW', {
      name: '妳好,{name}',
    });
    setList(getAllLocales());
  }, []);

from umi-next.

ycjcl868 avatar ycjcl868 commented on August 20, 2024
  1. 可以来一个获取当前所有可选语言的 API:umijs/umi#3789
  2. SSR 下的国际化,一般根据 url 来判断, getLocale 要支持根据 url 返回当前国际化。(类似 /zh-CN/index/en-US/index

from umi-next.

ycjcl868 avatar ycjcl868 commented on August 20, 2024

@chenshuai2144 formatMessage 会有问题,切换不了

from umi-next.

chenshuai2144 avatar chenshuai2144 commented on August 20, 2024

使用方式

import React, { useState } from "react";
import { Button } from "antd";
import { useIntl, getLocale, getAllLocales, setLocale } from "umi";
import styles from "./index.css";

export default function() {
  const intl = useIntl();
  const [list] = useState<string[]>(getAllLocales());
  const locale = getLocale();

  return (
    <div className={styles.normal}>
      <h1>当前语言:{locale}</h1>
      {list.map(locale => (
        <a
          onClick={() => {
            setLocale(locale);
          }}
          style={{
            margin: 8
          }}>
          {locale}
        </a>
      ))}
      <Button type="primary">
        {intl.formatMessage(
          {
            id: "name"
          },
          {
            name: "旅行者"
          }
        )}
      </Button>
    </div>
  );
}

效果:

image

from umi-next.

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.