Unity插件I2Localization源码分析

本地化的基础流程

  1. 获得本地化字符串LanguageSource.GetTranslationLocalizationManager.GetTermTranslation
  2. 如果属于资源类就LanguageSource.FindAssetLocalizationManager.FindAsset

LocalizationManager

  1. 记录所有实例
    • 所有LanguageSource,用于访问到所有本地化数据
    • 所有注册的ILocalizeTarget,用于处理对不同类型脚本的具体本地化逻辑
    • 所有注册的ILocalizationParamsManager,用于处理{[???]}参数的嵌入逻辑
  2. 提供主要静态方法
    • 本地化方法如:LocalizeAll,GetTermTranslation,GetAppName,ApplyLocalizationParams
    • 工具方法如:ApplyRTLfix
    • 资源方法如:FindAsset

LanguageSource

翻译数据的存储位置

  1. TermData
    • 本地化文本存储的类
    • 无论什么类型的资源,都会最终存为字符串类型。如果为资源类型,则需要再查找资源。
      • 如:sprite会存成资源的名字xxx。在使用的时候,再去本地化系统中的Assets里边去查找
  2. LanguageData
    • 语言配置
  3. Assets
    • 存储当前LanguageSource用到的资源

Localize 和 ILocalizeTarget

ILocalizeTarget为不同类型脚本的具体汉化逻辑。Localize在初始化的时候会从LocalizationManager获得当前适用的ILocalizeTarget

层次结构如下

public abstract class ILocalizeTarget
public abstract class LocalizeTarget<T> : ILocalizeTarget where T : Object
public class LocalizeTarget_UnityUI_Text : LocalizeTarget<UnityEngine.UI.Text> {}

public class LocalizeTarget_UnityUI_Image : LocalizeTarget<UnityEngine.UI.Image> 
{
    public override void GetFinalTerms ( Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm )
    {
        // 获取需要的脚本
        var mTarget = GetTarget(cmp);
        // 用mainTexture.name作为本地化key
        primaryTerm = mTarget.mainTexture ? mTarget.mainTexture.name : "";
        if (mTarget.sprite!=null && mTarget.sprite.name!=primaryTerm)
            primaryTerm += "." + mTarget.sprite.name;

        secondaryTerm = null;
    }


    public override void DoLocalize ( Localize cmp, string mainTranslation, string secondaryTranslation )
    {
        var mTarget = GetTarget(cmp);

        // 对物体进行本地化
        Sprite Old = mTarget.sprite;
        if (Old==null || Old.name!=mainTranslation)
            mTarget.sprite = cmp.FindTranslatedObject<Sprite>( mainTranslation );
    }
}

参数的处理

You have {[VALUE1]} points.
通过{[???]}的形式存储的字符串,通过正则查找出来({\[(.*?)\]}
在执行本地化的时候,会查找全局的ILocalizationParamsManager。进匹配成功的参数进行替换。

复数的处理

You have {[VALUE1]} points.[i2p_Zero]You have no points.[i2p_One]You have 1 point.
如果一个翻译串里边出现{[VALUE]}一类的参数,就可以设置复数形式。内部存储如上。

if (pluralType != null) 
{
    var tag = "[i2p_" + pluralType + "]";
    idx0 = translation.IndexOf (tag, System.StringComparison.OrdinalIgnoreCase);
    if (idx0 < 0) idx0 = 0;
             else idx0 += tag.Length;

    idx1 = translation.IndexOf ("[i2p_", idx0+1, System.StringComparison.OrdinalIgnoreCase);
    if (idx1 < 0) idx1 = translation.Length;

    translation = translation.Substring(idx0, idx1-idx0);
}

上述代码查找i2p的标签进行字符串裁剪

这是目前Unity最完整的本地化系统。 支持 Unity UI,Unity 2D,TextMesh Pro,NGUI,2D ToolKit,SVG导入器,标准组件 本地化 图像,文本,声音,字体,精灵,地图集,预制件,TextMeshes,下拉列表等。 自动翻译 内置支持Google Translator,可自动将所有标签本地化为任何语言。 Google Spreadsheets和CSV文件 链接外部电子表格,即使在游戏发布后,也会自动下载任何更改。 检测并修复错误 解析场景以查找丢失或未使用的翻译以及重复或类似的术语。 子对象翻译 设置辅助术语不仅可以更改文本和图像,还可以更改每种语言使用的字体和地图集。 回调和参数 可以修改翻译以使用语法语法插入分数,名称和游戏变量。 复数 每种语言的内置规则涵盖那些只需要单数/复数形式的规则,直到那些使用6种变体的规则。 从右到左语言 正确呈现RTL语言,支持换行和对齐。 保存内存 仅使用您正在使用的字符创建字体。链接您的AssetBundles以添加更多本地化内容。 简单高效 在一个位置编辑和预览所有翻译,然后在运行时仅加载当前语言。使用快速查找来访问它。 重复使用翻译 设置文本自动显示为大写,小写,上限,标题大小等。 在游戏动态翻译 Easy API在运行时翻译聊天消息和其他动态文本 应用程序名称和商店本地化 翻译游戏名称所有语言,并在Android / IOS商店中显示您的游戏支持哪些语言 简单但功能强大的编辑器 直观的检查员允许您预览,编辑,分类,创建,解析和过滤甚至大型来源。 编译时间检查 将翻译转换为脚本常量,以避免在访问代码时使用Typos。 Playmaker 可以 快速,轻松,强大地访问本地元素和交换语言并与之交互的几种操作?没问题! 立即获取I2本地化,让您的游戏为世界做好准备!
This is currently the most complete localization system available for Unity. Supports Unity UI, Unity 2D, TextMesh Pro, NGUI, 2D ToolKit, SVG Importer, Standard Components Localizes Image, Text, Sounds, Fonts, Sprites, Atlases, Prefabs, TextMeshes, Dropdowns and more. Auto Translation Built-in support for Google Translator to automatically localize all your labels into any language. Google Spreadsheets and CSV files Link external Spreadsheets and any change will be automatically downloaded, even after the game is released. Detect and Fix Errors Parse scenes to find missing or unused translations as well as duplicated or similar terms. Sub-Object Translations Set Secondary terms to change not just text and images, but also which Fonts and Atlases are used per language. Callbacks and Parameters Translations can be modified to insert Scores, Names and Game Variables using the languages grammar. Plurals Built-in rules for each language covering those that just need singular/plural forms up to those that use 6 variants. Right to Left Languages Correct rendering of RTL languages with support for line wrapping and alignment. Save Memory Create Fonts with only the characters you are using. Link your AssetBundles to add more localized content. Easy and Efficient Edit and Preview all your translations in one location, then, at runtime only the current language is loaded. Using fast lookups to access it. Reuse Translations Set texts to be automatically shown as UPPER CASE, lower case, Upper first, Title Case, etc. In Game Dynamic Translations Easy API to translate chat messages and other dynamic texts at runtime App Name and Store Localization Translate the name of your game to all languages and show in the Android/IOS store which languages your game support Simple yet Powerful Editors Intuitive inspectors allow you Preview, Edit, Categorize, Create, Parse and Filter even large sources. Compile Time Checking Bake translations into script constants to avoid Typo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值