Hi, I’m making my first videogame in Unity. I want it to be available in multiple languages, so I created this:
using UnityEngine;
using System.Collections.Generic;
[System.Serializable]
internal class LanguageStrings : Dictionary<string, string>
{
}
public static class LanguageSystem
{
public static Language language { get; private set; }
private static LanguageStrings strings;
public static void LoadLanguage(Language lang)
{
// Code to load language strings from JSON file...
language = lang;
}
public static string GetString(string key)
{
return !(strings == null || !strings.ContainsKey(key)) ? strings[key] : "<invalid>";
}
public enum Language
{
it,
en,
fr,
de
}
}
Essentially, whenever text needs to be displayed on screen, instead of displaying a literal string, it gets one from the Language System from a key. Like, for example, if I want to display the “Play” button in the main menu, the text string would be equal to LanguageSystem.GetString("menu_play"), which then returns the “Play” string if the current language is English, or another word if the language is changed.
Now what is the best place to store the strings? Do I write them directly in code (like a public static Dictionary<string, string> english { get; } or in a JSON file (or something else)?
I thought of writing them in JSON files inside a lang folder. But where do I put this folder? Do I just put it into my Assets folder? If I do, what happens to those files when I build the game? Will they appear in the built game folder along with the .exe? Will the code still be able to retrieve the files?
Also, what is the best way to retrieve the files from code? Because I need to load and deserialize the JSON file within the LoadLanguage method. I’ve heard of the Resources class and Resources.Load, but I don’t know how that works and I’ve also heard it’s outdated and that I should use the Addressables system. Took a look at that, and it seems really overkill for this kind of stuff.
Any suggestions? I’m completely new to file and resource management in Unity (and pretty new to Unity in general as well).