Example #1
0
        /// <summary>
        /// Loads JSON language files from %programfolder%/lang/
        /// </summary>
        public static void Init()
        {
            translationData = new Dictionary <string, LanguageDataObject>();

            //Preload language
            LoadLanguageResource("en.json");
            LoadLanguageResource("es.json");
            LoadLanguageResource("fr.json");
            LoadLanguageResource("ja.json");
            LoadLanguageResource("ko.json");
            LoadLanguageResource("pl.json");
            LoadLanguageResource("pt-BR.json");

            //We still load them after, for people who want to add new languages or change existing ones.
            try
            {
                var dirFiles = Directory.GetFiles(JsonManager.ProgramFolder + "lang/", "*.json");

                foreach (string file in dirFiles)
                {
                    string fileName = Path.GetFileName(file);
                    string langName = fileName.Remove(fileName.Length - 5);

                    Debug.WriteLine("Trying: " + langName);

                    LanguageDataObject langObj = JsonManager.LoadFile <LanguageDataObject>("lang/" + fileName);

                    //Overwrite if it exists, otherwise add
                    if (translationData.ContainsKey(langName.ToLower()))
                    {
                        translationData[langName.ToLower()] = langObj;
                    }
                    else
                    {
                        translationData.Add(langName.ToLower(), langObj);
                    }

                    Debug.WriteLine("Added Language: " + langName);
                }
            }
            catch (Exception)
            {
                //This is fine
            }
        }
Example #2
0
        private static void LoadLanguageResource(string fileName)
        {
            Assembly assembly     = Assembly.GetExecutingAssembly();
            string   resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(fileName));

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                if (stream == null)
                {
                    return;
                }

                using (StreamReader reader = new StreamReader(stream))
                {
                    string _data = reader.ReadToEnd();

                    string langName = fileName.Remove(fileName.Length - 5);

                    LanguageDataObject langObj = JsonManager.LoadString <LanguageDataObject>(_data);

                    translationData.Add(langName.ToLower(), langObj);
                }
            }
        }