0

I've got a bit of a weird issue going on right now. I am trying to load a localization json file from StreamingAssets. I am using Path.Combine to combine StreamingAssetsPath and Path together to form the full path. The value of Path is en-us. But for some reason Path.Combine is throwing a \ when concatting the strings so my path is invalid. If I change the value of Path to /en-US then it cuts the Application.StreamingAssetsPath portion off completely.

Debug.Log result of Path = en-US:

C:/Users/bluem/Documents/Fishtale/Assets/StreamingAssets\en-US

Debug.Log result of Path = /en-US

/en-US

I just cannot make heads or tales of this weirdness lol.

public void LoadLocalizedText()
    {
        localizedText = new Dictionary<string, string>();
        string filePath = Path.Combine(Application.streamingAssetsPath, path);

        Debug.Log(filePath);

        if (File.Exists(filePath))
        {
            string dataAsJson = File.ReadAllText(filePath);
            LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);

            for (int i = 0; i < loadedData.items.Length; i++)
            {
                localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
            }

            Debug.Log("Localization Manager: Data loaded, dictionary contains: " + localizedText.Count + " entries.");
        }
        else
        {
            Debug.LogError("Localization Manager: Cannot find data file name: " + filePath);
            return;
        }

        isReady = true;
    }
Jeremy Voss
  • 43
  • 1
  • 9
  • It's one of those nasty C# quirks :), you can read about it here: https://stackoverflow.com/a/1141114/3073551, the gotcha is that the second parameter is rooted, there is also the new `Path.Join` method which does not have this behavior, you can read about that here: https://stackoverflow.com/a/52486005/3073551 – MarkovskI Sep 30 '18 at 17:45
  • What's wrong with the first result other than the mixture of forward and back slashes? Windows paths can use either and can also use a combination thereof. It's putting it in as \ because that is the default directory separator (/, like your streamingAssetsPath is using, is the AltDirectorySeperator and frankly just looks weird to me as a windows guy) – pinkfloydx33 Sep 30 '18 at 21:08

1 Answers1

0

I will answer my own question. The responses provided were helpful, but not the solution to the problem.

The problem is not actually in the code, but rather the reference to the file being passed. The file needs to include the extension of .json or it simply will not find it. So in this case the answer is to change the value of Path from en-US to en-US.json.

Jeremy Voss
  • 43
  • 1
  • 9