前端国际化:别让你的应用只懂一种语言

前端国际化:别让你的应用只懂一种语言

毒舌时刻

这应用写得跟方言似的,出了本地就没人懂。

各位前端同行,咱们今天聊聊前端国际化。别告诉我你的应用还只有中文版本,那感觉就像在国际会议上只说方言——能说,但没人懂。

为什么你需要国际化

最近看到一个项目,想拓展海外市场,但所有文本都是硬编码在代码里的。我就想问:你是在做本地应用还是在做国际产品?

反面教材

// 反面教材:硬编码文本 function App() { return ( <div> <h1>欢迎来到我的网站</h1> <p>这是一个示例应用</p> <button>点击我</button> <div> <h2>产品列表</h2> <ul> <li>产品 1</li> <li>产品 2</li> <li>产品 3</li> </ul> </div> </div> ); } export default App; 

毒舌点评:这代码,就像在写日记,只有自己能看懂。

正确姿势

1. i18next

// 正确姿势:i18next // 1. 配置 // i18n.js import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; const resources = { zh: { translation: { welcome: '欢迎来到我的网站', description: '这是一个示例应用', button: '点击我', products: '产品列表', product1: '产品 1', product2: '产品 2', product3: '产品 3' } }, en: { translation: { welcome: 'Welcome to my website', description: 'This is a sample application', button: 'Click me', products: 'Product List', product1: 'Product 1', product2: 'Product 2', product3: 'Product 3' } } }; i18n .use(initReactI18next) .init({ resources, lng: 'zh', fallbackLng: 'en', interpolation: { escapeValue: false } }); export default i18n; // 2. 使用 // App.jsx import React from 'react'; import { useTranslation } from 'react-i18next'; function App() { const { t, i18n } = useTranslation(); const changeLanguage = (lng) => { i18n.changeLanguage(lng); }; return ( <div> <div> <button onClick={() => changeLanguage('zh')}>中文</button> <button onClick={() => changeLanguage('en')}>English</button> </div> <h1>{t('welcome')}</h1> <p>{t('description')}</p> <button>{t('button')}</button> <div> <h2>{t('products')}</h2> <ul> <li>{t('product1')}</li> <li>{t('product2')}</li> <li>{t('product3')}</li> </ul> </div> </div> ); } export default App; // 3. 入口文件 // index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import './i18n'; ReactDOM.render( <React.StrictMode> <App /> </React.StrictMode>, document.getElementById('root') ); 

2. react-intl

// 正确姿势:react-intl // 1. 配置 // App.jsx import React from 'react'; import { IntlProvider, FormattedMessage } from 'react-intl'; const zhMessages = { welcome: '欢迎来到我的网站', description: '这是一个示例应用', button: '点击我', products: '产品列表', product1: '产品 1', product2: '产品 2', product3: '产品 3' }; const enMessages = { welcome: 'Welcome to my website', description: 'This is a sample application', button: 'Click me', products: 'Product List', product1: 'Product 1', product2: 'Product 2', product3: 'Product 3' }; function App() { const [locale, setLocale] = React.useState('zh'); const messages = locale === 'zh' ? zhMessages : enMessages; return ( <IntlProvider locale={locale} messages={messages}> <div> <div> <button onClick={() => setLocale('zh')}>中文</button> <button onClick={() => setLocale('en')}>English</button> </div> <h1><FormattedMessage /></h1> <p><FormattedMessage /></p> <button><FormattedMessage /></button> <div> <h2><FormattedMessage /></h2> <ul> <li><FormattedMessage /></li> <li><FormattedMessage /></li> <li><FormattedMessage /></li> </ul> </div> </div> </IntlProvider> ); } export default App; 

3. 日期和数字格式化

// 正确姿势:日期和数字格式化 import React from 'react'; import { useTranslation } from 'react-i18next'; function FormattingExample() { const { t, i18n } = useTranslation(); const formatDate = (date) => { return new Intl.DateTimeFormat(i18n.language).format(date); }; const formatNumber = (number) => { return new Intl.NumberFormat(i18n.language).format(number); }; return ( <div> <p>{t('currentDate')}: {formatDate(new Date())}</p> <p>{t('price')}: {formatNumber(12345.67)}</p> </div> ); } export default FormattingExample; 

毒舌点评:这才叫国际化,让你的应用在全球范围内都能被理解。

Read more

2.2 基于ultrascale 架构FPGA的system manager wizard使用(温压监测)

2.2 基于ultrascale 架构FPGA的system manager wizard使用(温压监测)

Reference: 《PG185》《UG580》 部分文案源于网友博客,AIGC和个人理解,如有雷同纯属抄袭 一、介绍 简述: Xilinx System Management Wizard 是 Vivado 和 Vitis 工具中的一个图形化配置工具,主要用于为 FPGA 设计生成与系统监控和管理相关的 IP 核。这个工具帮助用户配置和集成诸如温度监控、电压监控、时钟监控、外部模拟输入等功能到 FPGA 设计中。它支持AXI4-Lite 与 DRP 接口 主要功能: * 温度和电压监测: * 内建传感器:支持 FPGA 内部温度、VCCINT(核心电压)、VCCAUX(辅助电压)、VCCBRAM(BRAM 电压)等电压和温度监测。通过 SYSMON 进行实时数据采集。

如何快速掌握GGCNN:机器人抓取检测的终极实战手册

如何快速掌握GGCNN:机器人抓取检测的终极实战手册 【免费下载链接】ggcnnGenerative Grasping CNN from "Closing the Loop for Robotic Grasping: A Real-time, Generative Grasp Synthesis Approach" (RSS 2018) 项目地址: https://gitcode.com/gh_mirrors/gg/ggcnn GGCNN(Generative Grasping Convolutional Neural Network)是一个革命性的机器人抓取检测开源项目,它通过深度学习技术实现了高精度的抓取位置和方向预测。该项目采用创新的生成式抓取合成方法,能够在动态环境中实时处理深度图像数据,为工业自动化、智能仓储等场景提供完整的智能抓取解决方案。GGCNN机器人抓取检测技术为机器人在复杂环境中的自主操作提供了强有力的支持。 🎯 GGCNN核心优势解析 轻量化网络架构设计 GGCNN采用全卷积网络架构,能够在单个前向传递中预测每个像素的抓取质量、角度和宽度。这种设计使

Rokid 手势识别技术深度解析:解锁 AR 无接触交互的核心秘密

Rokid 手势识别技术深度解析:解锁 AR 无接触交互的核心秘密

引言 在聊手势识别前,咱们先搞清楚:Rokid是谁?它为啥能把AR手势做得这么自然? Rokid是国内AR(增强现实)领域的“老兵”了,从2014年成立就盯着一个目标——让AR走进日常。你可能见过它的产品:能戴在脸上的“AR眼镜”Max Pro、能揣在兜里的“AR主机”Station 2、适合专业场景的“Station Pro”,这些设备不是用来“炫技”的,而是想让咱们摆脱手机、手柄的束缚,直接用手“摸”虚拟东西。 而手势识别,就是Rokid给AR设备装的“最自然的遥控器”——比如调大虚拟屏幕像捏橡皮一样捏合手指,翻页像翻书一样挥手。但不同设备、不同开发需求,需要搭配不同版本的SDK(软件开发工具包),这就像“不同型号的手机要装对应版本的APP”。 一、基础认知:先选对版本,避免开发走弯路 Rokid手势识别技术随SDK版本迭代持续优化,不同版本适配的Unity(开发工具)

OpenClaw大龙虾机器人完整安装教程

OpenClaw(大龙虾机器人)是一款本地部署的全能AI助手,可通过WhatsApp、Telegram、飞书等聊天软件实现邮件处理、日历管理、系统操作等功能,数据本地存储更隐私。本教程适配macOS/Linux/Windows系统,包含基础安装、初始化配置、聊天软件对接及常见问题解决,新手也能快速上手。 一、安装前准备 1. 系统与硬件要求 配置项最低要求推荐配置操作系统macOS 12+/Ubuntu 20.04+/Windows 10(需WSL2)macOS 14+/Ubuntu 22.04+/Windows 11内存4GB8GB+磁盘空间2GB可用10GB+ SSD核心依赖Node.js 18.0+Node.js v22 LTS最新版 2. 必备前置资源 * AI模型API Key:Claude、GPT-4/