Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting Localization data in Android with UnityWebRequest

Discussion in 'Scripting' started by kaiz12, Oct 19, 2019.

  1. kaiz12

    kaiz12

    Joined:
    Jun 1, 2013
    Posts:
    4
    Hi I used Unity localization tutorial to localize text in my app - In editor it works fine, but in Android it works partly (first canvas it doesn't find strings - shows missingTextString and other canvas is translated fine(buttons)).
    In the code I use missingTextString to debug errors too. It's more handy.


    So the code
    Code (CSharp):
    1. IEnumerator LoadLocalizedTextOnAndroid(string fileName)
    2.     {
    3.         localizedText = new Dictionary<string, string>();
    4.         string filePath;
    5.         filePath = Path.Combine(Application.streamingAssetsPath + "/", fileName);
    6.         missingTextString = "Android filePath: "+filePath;
    7.         string dataAsJson;
    8.         if (filePath.Contains("://") || filePath.Contains(":///"))
    9.         {
    10.             UnityWebRequest uwr = UnityWebRequest.Get(filePath);
    11.             yield return uwr.SendWebRequest();
    12.             if (uwr.isNetworkError || uwr.isHttpError)
    13.             {
    14.                 Debug.Log(uwr.error);
    15.                 dataAsJson = "error";
    16.                 missingTextString += uwr.error;
    17.             }
    18.             else
    19.             {
    20.                 dataAsJson = uwr.downloadHandler.text;
    21.                 missingTextString = missingTextString+" - "+ uwr.downloadHandler.text+" Juhuu";
    22.             }
    23.          
    24.         }
    25.         else
    26.         {
    27.             dataAsJson = File.ReadAllText(filePath);
    28.             Debug.Log("DataAsJson = "+dataAsJson);
    29.             missingTextString += dataAsJson;
    30.         }
    31.         LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);
    32.  
    33.         for (int i = 0; i < loadedData.items.Length; i++)
    34.         {
    35.             localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
    36.             Debug.Log("KEYS:" + loadedData.items[i].key);
    37.         }
    38.  
    39.         isReady = true;
    40.     }
     
  2. kaiz12

    kaiz12

    Joined:
    Jun 1, 2013
    Posts:
    4
    Sry if it helps Unity version is 2019.2.0f1 and I'm testing in Android is 8.1