Unity powerful configuration plug-in - Luban use (2) localization


foreword

Localization/Multilingual is a necessary function for a mature game, English is localization, referred to as l10n.
Attached: official documents .
The following operations require pre-preparation for the first article, click here to return to the previous chapter .


1. Localization configuration

1. Localization configuration rules

l10n:input_text_files 本地化映射文件
l10n:text_field_name 映射的目标字段名。因为有可能多个语言都在同一个映射表内(如text_tw和text_en)
l10n:output_not_translated_text_file 未完成文本值本地化映射的text

Open the .bat file and add the following three lines under -s all ^

set WORKSPACE=..
...
...
 -s all  ^
--l10n:input_text_files Config\Datas\l10n\TextTable_CN.xlsx ^
--l10n:text_field_name text_en ^
--l10n:output_not_translated_text_file NotLocalized_CN.txt

 pause

2. Add a localized key value and default value to any attribute of the sample configuration table. The key value is modified according to the project, and each key corresponds to a default value
insert image description here

3. Create a new localization table Config\Datas\l10n\TextTable_CN.xlsx and fill in the key value and the corresponding language value; conveniently create an unfinished localized text Config\Datas\l10n\NotLocalized_CN.txt, 4. Run
insert image description here
successfullyinsert image description here

Two, loading

1. Official loading method

/// 用于切换到en
string TextMapper_en(string key, string originText) 
{
    
    
    return tables.TbTextMapper.GetOrDefault(key)?.TextEn ?? originText;
}

/// 用于切换到tw
string TextMapper_tw(string key, string originText) 
{
    
    
    return tables.TbTextMapper.GetOrDefault(key)?.TextTw ?? originText;
}

void SwitchLanguages(cfg.Tables tables)
{
    
    
  // 切换所有text字段到 en
   tables.TranslateText(TextMapper_en);

  // 切换所有text字段到 tw
   tables.TranslateText(TextMapper_tw);

  // 当前语言为tw的情况下,不使用切换,
  // 获得道具1001的desc字段对应的英语文本
  var desc_en = table.TbTextMapper.Get(tables.TbItem.Get(1001).Desc_l10n_key).TextEn;
}

2. Single table loading
If you complete the various operations of the single table according to the configuration in Chapter 1, then the multilingual attribute is EditorText, and we need the attribute .Text to get the real value.
Specific projects are managed in several bats or configured in Jenkins. Multi-language can be extended to GetVOData in DataManager (add preconditions according to different directories).
Bat configures different directories:

set WORKSPACE=..
 
set GEN_CLIENT=%WORKSPACE%\Luban\Tools\Luban.ClientServer\Luban.ClientServer.exe
set CONF_ROOT=%WORKSPACE%\Luban\Config
 
%GEN_CLIENT% -j cfg --^
 -d %CONF_ROOT%\Defines\__root__.xml ^
 --input_data_dir %CONF_ROOT%\Datas ^
 --output_code_dir %WORKSPACE%/Assets/Scripts/Game/0.Model/Base ^
 --output_data_dir ..\Assets\StreamingAssets\tw ^
 --gen_types code_cs_unity_json,data_json ^
 -s all  ^
--l10n:input_text_files Config\Datas\l10n\TextTable_CN.xlsx ^
--l10n:text_field_name text_tw ^
--l10n:output_not_translated_text_file NotLocalized_CN.txt
 
%GEN_CLIENT% -j cfg --^
 -d %CONF_ROOT%\Defines\__root__.xml ^
 --input_data_dir %CONF_ROOT%\Datas ^
 --output_code_dir %WORKSPACE%/Assets/Scripts/Game/0.Model/Base ^
 --output_data_dir ..\Assets\StreamingAssets\en ^
 --gen_types code_cs_unity_json,data_json ^
 -s all  ^
--l10n:input_text_files Config\Datas\l10n\TextTable_CN.xlsx ^
--l10n:text_field_name text_en ^
--l10n:output_not_translated_text_file NotLocalized_CN.txt
pause

DataManager adds multilingual table loading

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using cfg;

namespace QFramework.Game
{
    
    
    public class DataManager : MonoSingleton<DataManager>
    {
    
    
        readonly Dictionary<string, object> tables = new Dictionary<string, object>();

        public T GetVOData<T>(string fileName,string language = "cn") where T : IVOFun, new()
        {
    
    
            var path = Application.streamingAssetsPath +"/" + language + "/" + fileName + ".json";

            if (tables.ContainsKey(fileName))
            {
    
    
                return (T)tables[fileName];
            }
            else
            {
    
    
                var data = new T();

                if (File.Exists(path)) {
    
    
                    //创建一个StreamReader,用来读取流
                    StreamReader sr = new StreamReader(path);
                    //将读取到的流赋值给jsonStr
                    string text = sr.ReadToEnd();
                    data._LoadData(text);
                    tables.Add(fileName, data);
                }
                else
                {
    
    
                    Debug.LogError("存档文件不存在");
                }
                return data;
            }
        }
    }
}

3. Unconfigured problems

1. Since we defined 10 data in the item table, the translation only translated the first 9, so the 10th element was marked in the incomplete localization.
insert image description here
On the other side, the tenth element shows the default value 2、
2. If you need other fonts, such as traditional Chinese, replace –l10n:text_field_name text_en ^ in the bat with –l10n:text_field_name text_tw ^ and click again to generate traditional json


Summarize

Luban has a lot of powerful functions, but I can’t use them, I can only improve according to my own ideas and talk about what’s not there; the details of luban are more modified according to specific projects, I hope you can configure them for you Luban library, escape the shackles of handwritten json.

Guess you like

Origin blog.csdn.net/qq_41912124/article/details/129004787