Y
Y
Yura Berezovsky2018-08-22 17:13:56
Android
Yura Berezovsky, 2018-08-22 17:13:56

How to access my language pack on android?

There is an xml file reader, but it requires a file path. I stuffed LanguageDictionary.xml into StreamingAssets, and wrote paths for all systems. But in the build it still does not find the language file. What are they doing?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
//https://toster.ru/q/271453
public class Lang
    {
        private Hashtable Strings;
   
    public Lang(string path, string language)
    {      
            SetLanguage(path, language);   
    }

    public void SetLanguage(string path, string language)
        {
            
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            Strings = new Hashtable();
            var element = xml.DocumentElement[language];
            if (element != null)
            {
                var elemEnum = element.GetEnumerator();
                while (elemEnum.MoveNext())
                {
                    var xmlItem = (XmlElement)elemEnum.Current;
                    Strings.Add(xmlItem.GetAttribute("name"), xmlItem.InnerText);
                }
            }
            else
            {
                Debug.LogError("The specified language does not exist: " + language);
            }
        }


        public string GetString(string name)
        {
            if (!Strings.ContainsKey(name))
            {
                Debug.LogError("The specified string does not exist: " + name);

                return "";
            }

            return (string)Strings[name];
        }
    }

private void Update()
    {
//Должен засветиться определенный цвет если языковый пакет найден
        if (Application.platform == RuntimePlatform.Android)
        {
            if (File.Exists("jar:file://" + Application.dataPath + "!/assets/" + "/LanguageDictionary.xml"))
            {
                debugImg.color = new Color(0, 0, 0);
            }
        }
        else if (Application.platform == RuntimePlatform.OSXEditor)
        {
            if (File.Exists(Application.dataPath + "/StreamingAssets" + "/LanguageDictionary.xml"))
            {
                debugImg.color = new Color(0, 173, 0);
            }
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            if (File.Exists(Application.dataPath + "/StreamingAssets" + "/LanguageDictionary.xml"))
            {
                debugImg.color = new Color(0, 0, 175);
            }

        }
        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            if (File.Exists(Application.dataPath + "/Raw" + "/LanguageDictionary.xml"))
            {
                debugImg.color = new Color(123, 123, 0);
            }

        }

    }

5b7d6f7b49c79992024221.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2018-08-22
@Jurajkeee

On Android, File. Ship via www. By the way, your path is not to streaming assets for android. Use Application.streamingAssetsPath

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question