• @umijs/plugin-locale使用


    介绍

    plugin-locale是一个国际化的插件,用于解决i18n问题,约定式的多语言支持,可以进行多个国际语言的切换

    启用方式:

    在umirc.ts文件中配置locale:{}开启

    使用

    在src下创建一个locales文件夹,在文件夹下配置我们的语言文件

    中文语言文件:zh-CN.js

    1. export default {
    2. WELCOME_TO_UMI_WORLD: '{name},欢迎光临umi的世界',
    3. };

    英文语言文件:en-US.js

    1. export default {
    2. WELCOME_TO_UMI_WORLD: "{name}, welcome to umi's world",
    3. };

    注意:如果项目配置了 singular: true ,locales 要改成 locale

    App.ts 配置

    1. import zhTW from 'antd/es/locale/zh_TW';
    2. import {addLocale} from 'umi'
    3. // 动态增加新语言
    4. addLocale(
    5. 'zh-TW',
    6. {
    7. // id 列表
    8. name: '妳好,{name}',
    9. },
    10. {
    11. momentLocale: 'zh-tw',
    12. antd: zhTW,
    13. },
    14. );

    动态的增加语言,增加语言之后可以通过getAllLocales获取列表

    addLocale 三个参数。

    • name 语言的 key。例如 zh-TW
    • message 语言的 id 列表。 例如:{ // id 列表 name: '妳好,{name}', }
    • 相应的 momentLocale 和 antd 配置

    配置完以上代码之后,我们需要重新运行一下项目,页面可能会报一些红色波浪线错误,但不影响使用,原因是ts类型问题,如果不想报红色波浪线,可以在后面加上:any,这是最快的解决方案,但是一般不推荐使用 

    在组件中使用

     getAllLocales

    获取当前获得所有国际化文件的列表,默认会在 locales 文件夹下寻找类似 en-US.(js|json|ts) 文件

    1. import { getAllLocales } from 'umi';
    2. console.log(getAllLocales()); // [en-US,zh-CN,...]

     getLocale

    获取当前选择的语言

    1. import { getLocale } from 'umi';
    2. console.log(getLocale()); // en-US | zh-CN

     useIntl

    useIntl 是最常用的 api,它可以获得 formatMessage 等 api 来进行具体的值绑定 

    1. import styles from './index.less';
    2. import { getAllLocales } from 'umi';
    3. import { useIntl} from 'umi';
    4. export default function IndexPage() {
    5. const intl = useIntl();
    6. console.log(intl);
    7. return (
    8. <div className={styles.title}>
    9. <h1>Page index</h1>
    10. <div>{intl.messages.WELCOME_TO_UMI_WORLD}</div>
    11. </div>
    12. );
    13. }

    通过useIntl可以把我们的语言文件中内容渲染到页面

    setLocale

     设置切换语言,默认会刷新页面,可以通过设置第二个参数为 false ,来实现无刷新动态切换

    1. import styles from './index.less';
    2. import { getAllLocales } from 'umi';
    3. import { useIntl, setLocale } from 'umi';
    4. export default function IndexPage() {
    5. const intl = useIntl();
    6. console.log(intl);
    7. console.log(getAllLocales()); // [en-US,zh-CN,...]
    8. return (
    9. <div className={styles.title}>
    10. <h1>Page index</h1>
    11. <div>{intl.messages.WELCOME_TO_UMI_WORLD}</div>
    12. <button
    13. onClick={() => {
    14. setLocale('zh-CN', false);
    15. }}
    16. >
    17. 切换中文
    18. </button>
    19. <button onClick={() => {
    20. setLocale('en-US', false);
    21. }}>切换英文</button>
    22. </div>
    23. );
    24. }

    给定了两个button按钮,可以做语言的切换

    以上是plugin-locale的简单操作,详情请查看umi官网@umijs/plugin-locale

  • 相关阅读:
    计算机图形学中的曲线问题——拉格朗日插值曲线绘制实践
    ncnn之三(补充):window环境下vs2022安装ncnn+protobuf
    云计算学习笔记——第三章 计算虚拟化[二]
    模型量化技术-INT8
    Nginx的安装
    Day727.键值数据库的基本架构 -Redis 核心技术与实战
    Java笔记:垃圾回收
    【21天Python进阶学习挑战赛】[day1]正则表达式大总结
    网友心得—运行jeecgboot-vue3项目可能出现的问题
    项目范围管理
  • 原文地址:https://blog.csdn.net/qq_60976312/article/details/127749984