Liberi
An exergame built for kids with CP!
Localizer.cs
1 using System;
2 using UnityEngine;
3 using System.Collections.Generic;
4 using System.Linq;
5 
9 public class Localizer : MonoBehaviour
10 {
14  public static SystemLanguage[] AvailableLanguages
15  {
16  get { return _localizedStrings.Keys.ToArray(); }
17  }
18 
19  static Dictionary<SystemLanguage, Dictionary<string, string>> _localizedStrings;
20 
21  void Awake ()
22  {
23  _localizedStrings = new Dictionary<SystemLanguage,Dictionary<string,string>>();
24 
25  foreach (var language in Enum.GetValues(typeof(SystemLanguage)))
26  {
27  var localizedStringsByLanguage = new Dictionary<string, string>();
28 
29  try
30  {
31  foreach (var stringsAsset in Resources.LoadAll<TextAsset>(
32  "Localization/" + language + "/Strings"))
33  {
34  var stringsJeli = UJeli.Parse(stringsAsset.text);
35 
36  foreach (var stringJeli in stringsJeli.Children)
37  {
38  localizedStringsByLanguage[stringJeli.Name] = stringJeli.Value.Replace("\\n", "\n");
39  }
40  }
41 
42  _localizedStrings.Add((SystemLanguage)language, localizedStringsByLanguage);
43  }
44  catch { continue; }
45  }
46  }
47 
55  public static string GetString (string key, SystemLanguage language = SystemLanguage.Unknown)
56  {
57  if (language == SystemLanguage.Unknown)
58  language = GameClient.Instance.Config.UI.Language;
59 
60  Dictionary<string, string> localizedLanguage = null;
61 
62  if (!_localizedStrings.TryGetValue(language, out localizedLanguage))
63  return key;
64 
65  string localizedString = null;
66 
67  if (!localizedLanguage.TryGetValue(key, out localizedString))
68  return key;
69 
70  return localizedString;
71  }
72 }
static string GetString(string key, SystemLanguage language=SystemLanguage.Unknown)
Gets the localized string for the given string key.
Definition: Localizer.cs:55
Localization manager.
Definition: Localizer.cs:9
static SystemLanguage[] AvailableLanguages
Gets an array of available languages for this game.
Definition: Localizer.cs:15
ClientConfig Config
Gets the configuration for this client.
Definition: GameClient.cs:176
static GameClient Instance
Gets the sole instance of the game client.
Definition: GameClient.cs:136
Main game client class.
Definition: GameClient.cs:16
Unity version of Jeli markup class.
Definition: UJeli.cs:10