Exemplo n.º 1
0
        private async Task useRegion(XRegion region)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            mDicts.Clear();
            if (region.JsonDicts != null && region.JsonDicts.Count > 0)
            {
                List <Task <I18NTable> > load_task = new List <Task <I18NTable> >();
                List <int> load_task_index         = new List <int>();
                for (var i = 0; i < region.JsonDicts.Count; i++)
                {
                    if (!region.JsonDicts[i].LoadPath.IsNullOrEmpty())
                    {
                        load_task.Add(this.getJsonDict(region.JsonDicts[i].LoadPath));
                        load_task_index.Add(i);
                    }
                }

                if (load_task.Count > 0)
                {
                    await Task.WhenAll(load_task);

                    for (var i = 0; i < load_task.Count; i++)
                    {
                        foreach (var item in load_task)
                        {
                            var    json_obj     = item.Result;
                            bool   base64_value = region.JsonDicts[load_task_index[i]].Base64Value;
                            string groupName    = region.JsonDicts[load_task_index[i]].GroupName;
                            if (groupName.IsNullOrEmpty())
                            {
                                groupName = I18NConst.DefaultGroupName;
                            }

                            foreach (var kv in json_obj.data)
                            {
                                if (!mDicts.ContainsKey(groupName))
                                {
                                    mDicts.Add(groupName, new Dictionary <string, string>());
                                }
                                if (!mDicts[groupName].ContainsKey(kv.k))
                                {
                                    if (base64_value)
                                    {
                                        try
                                        {
                                            string _value = kv.v.DecodeBase64();
                                            mDicts[groupName].Add(kv.k, _value);
                                        }
                                        catch
                                        {
                                            Debug.LogError($"[TinaX.I18N] Decode base64 error: Json: {region.JsonDicts[load_task_index[i]].LoadPath}   | Key: {kv.k}");
                                        }
                                    }
                                    else
                                    {
                                        mDicts[groupName].Add(kv.k, kv.v);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (region.AssetDicts != null && region.AssetDicts.Count > 0)
            {
                foreach (var item in region.AssetDicts)
                {
                    if (item.Asset == null)
                    {
                        continue;
                    }
                    string groupName = item.GroupName.IsNullOrEmpty() ? I18NConst.DefaultGroupName : item.GroupName;
                    foreach (var kv in item.Asset.data)
                    {
                        if (!mDicts.ContainsKey(groupName))
                        {
                            mDicts.Add(groupName, new Dictionary <string, string>());
                        }
                        if (!mDicts[groupName].ContainsKey(kv.k))
                        {
                            mDicts[groupName].Add(kv.k, kv.v);
                        }
                    }
                }
            }

            mRegionName = region.Name;
            XEvent.Call(I18NEventConst.OnUseRegion, region.Name, TinaX.Const.FrameworkConst.FrameworkEventGroupName);
        }
Exemplo n.º 2
0
        public async Task <bool> Start()
        {
            if (mInited)
            {
                return(true);
            }
            mConfig = XConfig.GetConfig <I18NConfig>(I18NConst.ConfigPath_Resources, AssetLoadType.Resources);
            if (mConfig == null)
            {
                mStartException = new XException("[TinaX.I18N]Load I18N config file failed. please cheak in ProjectSettings window.");
                return(false);
            }
            if (!mConfig.EnableI18N)
            {
                return(true);
            }
            if (mAssets == null)
            {
                mStartException = new XException("[TinaX.I18N]No service implements the built-in asset loading interface in Framework");
                return(false);
            }

            if (mConfig.Regions != null && mConfig.Regions.Count > 0)
            {
                bool    check_default       = !mConfig.DefaultRegion.IsNullOrEmpty();
                string  default_region_name = check_default ? mConfig.DefaultRegion.ToLower() : string.Empty;
                XRegion _default_region     = null;
                foreach (var item in mConfig.Regions)
                {
                    if (item.Name.IsNullOrEmpty())
                    {
                        continue;
                    }

                    bool _flag_1 = false;
                    if (check_default && _default_region == null)
                    {
                        if (item.Name.ToLower().Equals(default_region_name))
                        {
                            _default_region = item;
                            _flag_1         = true;
                        }
                    }

                    if (mConfig.AutomaticMatchingBySystemLanaguage)
                    {
                        bool flag_2 = false;
                        if (item.BindLanguage != null && item.BindLanguage.Count > 0)
                        {
                            foreach (var lang in item.BindLanguage)
                            {
                                if (lang == Application.systemLanguage)
                                {
                                    flag_2          = true;
                                    _default_region = item;
                                    break;
                                }
                            }
                        }
                        if (flag_2)
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (_flag_1)
                        {
                            break;
                        }
                    }
                }
                if (_default_region != null)
                {
                    try
                    {
                        await this.useRegion(_default_region);
                    }
                    catch (XException e)
                    {
                        mStartException = e;
                        return(false);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }

            //Debug code
            //foreach(var item in mDicts)
            //{
            //    item.Key.LogConsole();
            //    foreach(var kv in item.Value)
            //    {
            //        ("    " + kv.Key + "  |  " + kv.Value).LogConsole();
            //    }
            //}

            mInited = true;
            return(true);
        }
Exemplo n.º 3
0
        private async Task useRegion(XRegion region)
        {
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            mDicts.Clear();
            if (region.JsonDicts != null && region.JsonDicts.Count > 0)
            {
                List <Task <I18NTable> > load_task = new List <Task <I18NTable> >();
                List <int> load_task_index         = new List <int>();
                for (var i = 0; i < region.JsonDicts.Count; i++)
                {
                    if (!region.JsonDicts[i].LoadPath.IsNullOrEmpty())
                    {
                        load_task.Add(this.getJsonDict(region.JsonDicts[i].LoadPath));
                        load_task_index.Add(i);
                    }
                }

                if (load_task.Count > 0)
                {
                    await Task.WhenAll(load_task);

                    for (var i = 0; i < load_task.Count; i++)
                    {
                        foreach (var item in load_task)
                        {
                            var    json_obj     = item.Result;
                            bool   base64_value = region.JsonDicts[load_task_index[i]].Base64Value;
                            string groupName    = region.JsonDicts[load_task_index[i]].GroupName;
                            if (groupName.IsNullOrEmpty())
                            {
                                groupName = I18NConst.DefaultGroupName;
                            }

                            foreach (var kv in json_obj.data)
                            {
                                if (!mDicts.ContainsKey(groupName))
                                {
                                    mDicts.Add(groupName, new Dictionary <string, string>());
                                }
                                if (!mDicts[groupName].ContainsKey(kv.k))
                                {
                                    if (base64_value)
                                    {
                                        try
                                        {
                                            string _value = kv.v.DecodeBase64();
                                            mDicts[groupName].Add(kv.k, _value);
                                        }
                                        catch
                                        {
                                            Debug.LogError($"[TinaX.I18N] Decode base64 error: Json: {region.JsonDicts[load_task_index[i]].LoadPath}   | Key: {kv.k}");
                                        }
                                    }
                                    else
                                    {
                                        mDicts[groupName].Add(kv.k, kv.v);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (region.AssetDicts != null && region.AssetDicts.Count > 0)
            {
                foreach (var item in region.AssetDicts)
                {
                    if (item.Asset == null)
                    {
                        continue;
                    }
                    string groupName = item.GroupName.IsNullOrEmpty() ? I18NConst.DefaultGroupName : item.GroupName;
                    foreach (var kv in item.Asset.data)
                    {
                        if (!mDicts.ContainsKey(groupName))
                        {
                            mDicts.Add(groupName, new Dictionary <string, string>());
                        }
                        if (!mDicts[groupName].ContainsKey(kv.k))
                        {
                            mDicts[groupName].Add(kv.k, kv.v);
                        }
                    }
                }
            }

            m_RegionName = region.Name;
            this.m_CurrentLanguageNames = region.BindLanguage.Select(l => l.ToString()).ToArray();
            this.m_CurrentLanguages     = region.BindLanguage.ToArray();
            Debug.Log($"[{I18NConst.ServiceName}] {(IsCmnHans() ? "使用区域:" : "Use region: ")} <color=#{TinaX.Internal.XEditorColorDefine.Color_Emphasize_16}>{region.Name}</color>");
            XEvent.Call(I18NEventConst.OnUseRegion, region.Name, TinaX.Const.FrameworkConst.FrameworkEventGroupName);
        }