Skip to content

Instantly share code, notes, and snippets.

@koster
Last active May 21, 2025 21:21
Show Gist options
  • Save koster/099cf277384674c50d8a664ddf544d87 to your computer and use it in GitHub Desktop.
Save koster/099cf277384674c50d8a664ddf544d87 to your computer and use it in GitHub Desktop.
A compact loc system that needs no setup
// Just create a bunch LocString's anywhere
// change Loc.language and you're done
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reflection;
public class LocString
{
public string en;
public string ru;
Dictionary<string, FieldInfo> field = new Dictionary<string, FieldInfo>();
public string GetText()
{
if (!field.ContainsKey(Loc.language))
{
Type type = this.GetType();
FieldInfo fieldInfo = type.GetField(Loc.language);
field.Add(Loc.language, fieldInfo);
}
return field[Loc.language].GetValue(this) as string;
}
public override string ToString()
{
return GetText();
}
}
public static class Loc
{
public const string LANG_EN = "en";
public const string LANG_RU = "ru";
public static string language = LANG_EN;
public static List<string> langs = new List<string>()
{
LANG_EN,
LANG_RU
};
public static LocString test_string = new LocString()
{
en = "I'm a test string",
ru = "Я тестовая строка"
};
}
@koster
Copy link
Author

koster commented Aug 1, 2024

label.text = Loc.test_string.ToString();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment