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

cocos create i18n 本地化

前言:

cocos create本身支持 i18n。用户起来也很友好。官方有自带的插件。很好就能实现

官网文档:文档地址

但是它本身只支持:

  • 文本(Label
  • 图片(Sprite)

我这边对插件进行了扩展,让他支持:

  • 富文本
  • 输入框(Placeholder)
  • 按钮
  • 文本支持占位符

这边刚好记录下。具体使用 官方文档已经有详细介绍了。我们主要从扩展的方面进行介绍。文章后面会上传对于代码

富文本扩展:

富文本的支持:

import * as i18n from './LanguageData'; import { _decorator, Component, Label, RichText } from 'cc'; const { ccclass, property, executeInEditMode } = _decorator; @ccclass('LocalizedRichText') @executeInEditMode export class LocalizedRichText extends Component { richText: RichText | null = null; @property({ visible: false }) key: string = ''; @property({ displayName: 'Key', visible: true }) get _key() { return this.key; } set _key(str: string) { this.updateLabel(); this.key = str; } onLoad() { if (!i18n.ready) { i18n.init('zh_cn'); } this.fetchRender(); } fetchRender () { let richText = this.getComponent('cc.RichText') as RichText; if (richText) { this.richText = richText; this.updateLabel(); return; } } updateLabel () { this.richText && (this.richText.string = i18n.t(this.key)); } }

使用和官方风格统一

输入框 扩展:

主要解决 占位提示:

import * as i18n from './LanguageData'; import { _decorator, Component, SpriteFrame, Sprite, Button, EditBox } from 'cc'; const { ccclass, property, executeInEditMode } = _decorator; @ccclass('LocalizedEditBox') @executeInEditMode export class LocalizedEditBox extends Component { editBox: EditBox | null = null; @property({ visible: false }) key: string = ''; @property({ displayName: 'Key', visible: true }) get _key() { return this.key; } set _key(str: string) { this.updateLabel(); this.key = str; } onLoad() { if (!i18n.ready) { i18n.init('zh_cn'); } this.fetchRender(); } fetchRender() { let editBox = this.getComponent('cc.EditBox') as EditBox; if (editBox) { this.editBox = editBox; this.updateLabel(); return; } } updateLabel() { this.editBox && (this.editBox.placeholder = i18n.t(this.key)); } }

使用和官方风格统一

按钮 扩展:

import * as i18n from './LanguageData'; import { _decorator, Component, SpriteFrame, Sprite, Button } from 'cc'; const { ccclass, property, executeInEditMode } = _decorator; @ccclass('LocalizedButtonItem') class LocalizedButtonItem { @property language: string = 'zh'; @property({ type: SpriteFrame, }) normalSprite: SpriteFrame | null = null; @property({ type: SpriteFrame, }) pressedSprite: SpriteFrame | null = null; } @ccclass('LocalizedButton') @executeInEditMode export class LocalizedButton extends Component { button: Button | null = null; @property({ type: LocalizedButtonItem, }) spriteList = []; onLoad() { if (!i18n.ready) { i18n.init('zh'); } this.fetchRender(); } fetchRender() { let button = this.getComponent('cc.Button') as Button; if (button) { this.button = button; this.updateSprite(); return; } } updateSprite() { for (let i = 0; i < this.spriteList.length; i++) { const item = this.spriteList[i]; // @ts-ignore if (item.language === i18n._language) { // @ts-ignore this.button.normalSprite = item.normalSprite; this.button.pressedSprite = item.pressedSprite break; } } } }

文本的占位符 扩展

使用文本:

/** * 占位符 * @param key * @param args * @returns */ export function format(key: string, ...args): string { const win: any = window; if (!win.languages) { return key; } const searcher = key.split('.'); let data = win.languages[_language]; for (let i = 0; i < searcher.length; i++) { data = data[searcher[i]]; if (!data) { return '-*-'; } } if (!data) { return '-*-'; } return data.replace(/\{(\d+)\}/g, (match, index) => { return args[index] !== undefined ? args[index] : match; }); }
http://www.jsqmd.com/news/502769/

相关文章:

  • 一键添加视频封面脚本
  • A4950驱动电路避坑指南:为什么你的震动电机不工作?实测8V电压阈值问题
  • 罗兰艺境GEO诊断与验证系统:品牌AI可见度的“测量基准仪”与“效果公证处” - 罗兰艺境GEO
  • 多维数组在算法设计中的存储映射问题的技术4
  • 写中国,就不能只写中国 - 速递信息
  • 论文排版效率革命:Paperxie 如何让高校学子告别格式繁琐
  • 【强化学习】GAIL:绕过奖励函数,直接模仿专家策略的博弈艺术
  • Maxwell中铜导体热损计算的关键步骤与技巧
  • 2026年极萌水光仪深度解析:基于口碑与效果的市场评价分析 - 外贸老黄
  • PPO与DQN在Replay Buffer使用上的本质差异——从重要性采样角度解析
  • 安卓手机版浏览器推荐!能下很多网页视频的浏览器
  • 如何在HTML/JavaScript中禁用Ctrl+C
  • 告别卡顿!VSCode自动补全加速的5个冷门设置(2024实测有效)
  • 2025年-2026年水光仪品牌推荐:居家高效护肤场景深度评测,解决吸收差与成本高痛点并附购买排名 - 外贸老黄
  • 2026硬核实测:一篇吃透降AI保姆级攻略(附工具红黑榜)
  • YMatrix 亮相 2026 用友全球生态大会 超融合 AI 数据库助力企业 AI 全面落地
  • PostgreSQL保姆级下载安装指南
  • Ralph for Claude Code 彻底移除指南:从系统清理到环境重置
  • Phi-3 Forest Lab保姆级教学:模型蒸馏实践——用Phi-3-mini指导更小模型
  • 特斯拉FSD和Waymo的决策逻辑有啥不同?聊聊规则模型与效用模型的实战选择
  • 2026年极萌大排灯深度解析:基于口碑与效果的技术权威分析 - 外贸老黄
  • 书籍推荐:三本经典书籍让你精通项目管理全流程
  • 广州痛症舒缓养生馆推荐|告别颈腰疼痛,选对场馆更省心 - 妙妙水侠
  • 即插即用系列 | 结构感知Mamba新范式:SCSegamba如何以1M参数量革新工业缺陷检测
  • WEB安全实战:从原理到工具,全面剖析逻辑越权漏洞的攻防
  • 2025年-2026年好用的美容仪品牌推荐:技术创新与市场价值双维度综合评估 - 外贸老黄
  • 聊聊@DSTransactional的坑
  • MedGemma Medical Vision Lab支撑AI伦理研究:非诊断场景下医学影像理解边界实证分析
  • ModuleNotFoundError: No module named ‘moviepy.editor‘,安装了的,版本不对
  • UN R155与ISO 21434:汽车信息安全法规与标准的协同实践指南