Beispiel #1
0
        /// <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 localisationFolder, 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 pathEnd = string.Format("{0}/{1}/{2}.{3}", localisationFolder, language, filename, PWConst.LANG_PK_EXTENSION);
            string path    = null;

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

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

            return(LanguagePack.Load(path));
        }
Beispiel #2
0
        /// <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]);
        }
Beispiel #3
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 localisationFolder, string className)
        {
            LanguagePack langPack = EditorLanguagePack.Load(localisationFolder, className, Language);

            if (langPack == null)
            {
                langPack = EditorLanguagePack.Load(localisationFolder, 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);
        }