当前位置: 首页 > news >正文

【前端国际化】ICU消息格式:处理复杂翻译场景

【前端国际化】ICU消息格式:处理复杂翻译场景

前言

大家好,我是cannonmonster01!上一篇我们聊了i18next的基本用法,今天咱们来深入聊聊ICU消息格式。如果你曾经遇到过复数处理、性别差异、日期格式化等复杂翻译场景,那么ICU消息格式就是你的救星!

什么是ICU消息格式

ICU(International Components for Unicode)是一套国际化支持库,ICU消息格式是其中的核心部分。它提供了一种强大的方式来处理复杂的翻译场景:

  • 复数形式(Plurals)
  • 性别形式(Gender)
  • 选择形式(Select)
  • 格式化(Formatting)
  • 嵌套消息(Nested)

ICU消息格式的语法

基本语法

{占位符名, 类型, 格式}

数据类型

类型说明示例
number数字格式化{count, number}
date日期格式化{date, date}
time时间格式化{time, time}
plural复数处理{count, plural, one {...} other {...}}
select选择处理{gender, select, male {...} female {...} other {...}}
selectordinal序数处理{rank, selectordinal, one {...} two {...}}

复数处理

基本用法

// 翻译字符串 const message = '{count, plural, one {1 item} other {{count} items}}'; // 使用 formatMessage(message, { count: 1 }); // "1 item" formatMessage(message, { count: 5 }); // "5 items"

复数类别

不同语言有不同的复数规则:

// 英语:one, other {count, plural, zero {No items} one {1 item} two {2 items} few {A few items} many {Many items} other {{count} items} }

实际示例

{ "items": "{count, plural, one {1件商品} other {{count}件商品}}", "apples": "{count, plural, zero {没有苹果} one {1个苹果} other {{count}个苹果}}", "visitors": "{count, plural, one {一位访客} other {{count}位访客}}" }

性别处理

基本用法

const message = '{gender, select, male {他} female {她} other {他们}}'; formatMessage(message, { gender: 'male' }); // "他" formatMessage(message, { gender: 'female' }); // "她" formatMessage(message, { gender: 'other' }); // "他们"

组合使用

const message = '{gender, select, male {他有 {count, plural, one {1个苹果} other {{count}个苹果}} } female {她有 {count, plural, one {1个苹果} other {{count}个苹果}} } other {他们有 {count, plural, one {1个苹果} other {{count}个苹果}} } }'; formatMessage(message, { gender: 'male', count: 5 }); // "他有5个苹果"

选择处理

基本用法

const message = '{role, select, admin {管理员} user {普通用户} guest {访客}}'; formatMessage(message, { role: 'admin' }); // "管理员" formatMessage(message, { role: 'user' }); // "普通用户"

复杂示例

{ "accessLevel": "{level, select, guest {访客权限 - 仅可浏览} user {用户权限 - 可编辑} admin {管理员权限 - 完全控制} superadmin {超级管理员 - 所有权限} }" }

格式化处理

数字格式化

// 基本数字 {price, number} // 1234.56 {price, number, integer} // 1235 {price, number, percent} // 123456% {price, number, currency} // $1,234.56 {price, number, #,##0.00} // 1,234.56

日期格式化

// 日期格式 {date, date} // 2024/01/15 {date, date, short} // 1/15/24 {date, date, medium} // Jan 15, 2024 {date, date, long} // January 15, 2024 {date, date, full} // Monday, January 15, 2024 {date, date, year} // 2024 {date, date, month} // January {date, date, day} // 15

时间格式化

// 时间格式 {time, time} // 10:30:00 AM {time, time, short} // 10:30 AM {time, time, medium} // 10:30:00 AM {time, time, long} // 10:30:00 AM GMT+8

嵌套消息

基本嵌套

const message = '欢迎{name}加入我们的社区!您是第{count, plural, one {1位} other {{count}位}}成员。'; formatMessage(message, { name: '小明', count: 100 }); // "欢迎小明加入我们的社区!您是第100位成员。"

多层嵌套

const message = '{gender, select, male {他是第 {rank, selectordinal, one {一} two {二} other {{rank}}} 位用户} female {她是第 {rank, selectordinal, one {一} two {二} other {{rank}}} 位用户} other {他们是第 {rank, selectordinal, one {一} two {二} other {{rank}}} 位用户} }';

在i18next中使用ICU

安装插件

npm install i18next-icu

配置

import i18n from 'i18next'; import ICU from 'i18next-icu'; import { initReactI18next } from 'react-i18next'; i18n .use(ICU) .use(initReactI18next) .init({ fallbackLng: 'zh', interpolation: { escapeValue: false } });

使用示例

// locales/zh/common.json { "items": "{count, plural, one {1件商品} other {{count}件商品}}", "welcome": "欢迎{name}!今天是{date, date, long}。", "balance": "您的余额是{amount, number, currency}。" }
import { useTranslation } from 'react-i18next'; const Component = () => { const { t } = useTranslation(); return ( <div> <p>{t('items', { count: 5 })}</p> <p>{t('welcome', { name: '小明', date: new Date() })}</p> <p>{t('balance', { amount: 1234.56 })}</p> </div> ); };

在Vue中使用ICU

import { createI18n } from 'vue-i18n'; import { ICUMessageFormatPlugin } from '@vue/composition-api'; const i18n = createI18n({ legacy: false, locale: 'zh', fallbackLocale: 'zh', messages: { zh: { items: '{count, plural, one {1件商品} other {{count}件商品}}' }, en: { items: '{count, plural, one {1 item} other {{count} items}}' } }, plugins: [ICUMessageFormatPlugin] });

复数规则详解

CLDR复数类别

类别说明适用语言
zero阿拉伯语等
one英语、法语等
two阿拉伯语、威尔士语
few少数俄语、波兰语等
many多数阿拉伯语、希腊语等
other其他所有语言

英语复数规则

// 英语:1用one,其他用other {count, plural, one {1 item} other {{count} items}}

俄语复数规则

// 俄语:1用one,2-4用few,其他用many {count, plural, one {{count} предмет} few {{count} предмета} many {{count} предметов}}

中文复数规则

// 中文:通常不需要复数变化 {count, plural, other {{count}件商品}}

最佳实践

保持翻译简洁

// 好的示例 {count, plural, one {1个苹果} other {{count}个苹果}} // 避免重复 {count, plural, one {有1个苹果} other {有{count}个苹果}}

使用默认值

// 提供默认值 {name, select, male {他} female {她} other {用户}}

测试各种场景

// 测试边界情况 test('复数处理', () => { expect(formatMessage('{count, plural, one {1} other {{count}}}', { count: 0 })).toBe('0'); expect(formatMessage('{count, plural, one {1} other {{count}}}', { count: 1 })).toBe('1'); expect(formatMessage('{count, plural, one {1} other {{count}}}', { count: 100 })).toBe('100'); });

工具支持

ICU Message Editor

这是一个在线工具,可以帮助你编写和测试ICU消息格式:

  • 官网:https://format-message.github.io/icu-message-editor/

i18next-parser配置

module.exports = { options: { interpolation: { formatSeparator: ',' }, parseICU: true } };

总结

ICU消息格式是处理复杂翻译场景的利器,通过今天的学习,相信你已经掌握了:

  1. ICU消息格式的基本语法
  2. 复数处理的用法
  3. 性别和选择处理
  4. 日期和数字格式化
  5. 嵌套消息的使用
  6. 在i18next和Vue中的集成

掌握ICU消息格式,让你的国际化应用更加专业和完善!

http://www.jsqmd.com/news/877397/

相关文章:

  • 别再让ChatGPT瞎编市场数据!商业计划书核心章节的11项权威信源对接指南(含Statista/IBISWorld/API直连方案)
  • 2026深度实测:16款降AIGC网站测评,闭眼入这款就对了!
  • 终极指南:用BG3 Mod Manager轻松管理《博德之门3》模组,告别游戏崩溃
  • Zabbix监控国产化:一篇搞定银河麒麟V10系统下Agent的编译、配置与开机自启
  • ComfyUI-WanVideoWrapper完全指南:轻松实现专业级AI视频生成
  • 终极音乐解锁指南:免费快速解锁加密音乐文件的完整方案
  • Realtek USB网卡驱动实战:5分钟解锁NAS网络扩展新姿势
  • 打破性能与可解释性权衡:GAMs模型实战评估与选择指南
  • 2026降AIGC革命:降AIGC工具实测TOP榜与安全选型攻略
  • CompressO:免费开源的专业视频压缩工具,让大文件秒变小
  • Windows上安装APK的秘密武器:APK-Installer如何颠覆你的跨平台体验?
  • 如何快速掌握Vosk API:离线语音识别的完整实战指南
  • 五分钟完成Python环境接入Taotoken调用多模型API
  • LyricsX终极指南:macOS上最智能的歌词同步体验
  • 3分钟学会:永久保存B站缓存视频的完整教程
  • 线性菲涅尔式太阳能聚光系统的优化设计及性能方法【附程序】
  • 多源数据协同与智能算法融合的煤矿工作面透明化系统【附程序】
  • 如何快速安装MASA全家桶汉化包:Minecraft模组中文界面终极指南
  • ChatGPT记忆功能怎么用:2024年Q2最新限制已生效!3类账号权限差异+2种绕过合规路径(限内部测试版)
  • 医疗AI数据验证与文档化:DAIMS框架实战指南
  • 详细解读Taotoken控制台中的用量分析与账单明细功能
  • 3大难题破解:Python-for-Android实战指南
  • 2026年5月最新丹棱县黄金回收白银回收铂金回收权威排行榜TOP5:纯金+金条+银条+钯金 门店地址联系方式推荐 - 莘州文化
  • 为 AI Agent 框架 OpenClaw 配置 Taotoken 作为模型供应商
  • 2026佛山市黄金回收行情实录,五家合规店铺口碑+免费上门 - 亦辰小黄鸭
  • Windows安卓应用运行解决方案:APK-Installer技术指南
  • Betaflight 2025.12深度解析:实时任务调度与USB协议栈架构重构实现无人机飞控系统性能突破
  • 如何永久备份微信聊天记录:3步完成完整数据导出的终极指南
  • 2026推荐:内江母婴除甲醛CMA甲醛检测治理公司推荐品牌排行榜 - 金诚回收
  • Sunshine虚拟控制器架构深度解析:多平台游戏串流输入技术实战指南