/// <summary>
        /// Loads a language pack and returns it, or null.
        /// </summary>
        /// <param name="path">Path of the lang pack file.</param>
        /// <returns>A language pack with the name and language, or null</returns>
        public static LanguagePack Load(string path)
        {
            if (ms_loadedPacks.ContainsKey(path) && ms_loadedPacks[path] != null)
            {
                return(ms_loadedPacks[path]);
            }

            LanguagePack langPack = new LanguagePack();

            BinaryFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // Version
                langPack.Version = (string)formatter.Deserialize(stream);

                switch (langPack.Version)
                {
                case "1":
                    langPack.LoadV1(formatter, stream);
                    ms_loadedPacks[path] = langPack;
                    break;

                default:
                    throw new ApplicationException("Unknown language pack v" + langPack.Version);
                }
            }

            langPack.m_path = path;
            return(ms_loadedPacks[path]);
        }
Exemple #2
0
        /// <summary>
        /// Get the language pack for the class with <paramref name="className"/>.
        /// If the language does not exist, the system will fallback to the <see cref="DEFAULT_LANGUAGE"/>
        /// </summary>
        /// <param name="className">Name of the class to get the lang pack for</param>
        /// <returns>Lang pack</returns>
        public static LanguagePack GetLanguagePackOrDefault(string className)
        {
            LanguagePack langPack = EditorLanguagePack.Load(className, Language);

            if (langPack == null)
            {
                langPack = EditorLanguagePack.Load(className, DEFAULT_LANGUAGE);

                if (Translate.Present)
                {
                    if (langPack != null)
                    {
                        Translate.MissingLanguagePack(className);
                    }
                }
            }

            if (langPack == null)
            {
                if (Dev.Present)
                {
                    Dev.NoLocalizationPkg(className, Culture.Language, Culture.DEFAULT_LANGUAGE);
                }
                else
                {
                    Debug.LogError("No localization package was found for " + className + "!");
                }
            }

            return(langPack);
        }
        /// <summary>
        /// Loads a language pack and returns it, or null.
        /// </summary>
        /// <param name="name">Name of the lang pack.</param>
        /// <param name="language">Language of the lang pack.</param>
        /// <returns>A language pack with the name and language, or null</returns>
        public static LanguagePack Load(string name, SystemLanguage language)
        {
            string filename = name + "." + language.ToString();

#if UNITY_EDITOR
            string[] guids = AssetDatabase.FindAssets(filename);
            if (guids == null || guids.Length < 1)
            {
                return(null);
            }
#endif

            string path = null;

#if UNITY_EDITOR
            // Find the first match
            foreach (var guid in guids)
            {
                string p = AssetDatabase.GUIDToAssetPath(guid);
                if (p.EndsWith("." + PWConst.LANG_PK_EXTENSION))
                {
                    path = p;
                    break;
                }
            }
#endif

            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            return(LanguagePack.Load(path));
        }