Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

//#define SHOW_REPORT

using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Localization manager is able to parse localization information from text assets.
/// Using it is simple: text = Localization.Localize(key), or just add a UILocalize script to your labels.
/// You can switch the language by using Localization.instance.currentLanguage = "French", for example.
/// This will attempt to load the file called "French.txt" in the Resources folder.
/// The localization file should contain key = value pairs, one pair per line.
/// </summary>

[AddComponentMenu("NGUI/Internal/Localization")]
public class Localization : MonoBehaviour
{
	static Localization mInstance;

	/// <summary>
	/// Whether there is an instance of the localization class present.
	/// </summary>

	static public bool isActive { get { return mInstance != null; } }

	/// <summary>
	/// The instance of the localization class. Will create it if one isn't already around.
	/// </summary>

	static public Localization instance
	{
		get
		{
			if (mInstance == null)
			{
				mInstance = Object.FindObjectOfType(typeof(Localization)) as Localization;

				if (mInstance == null)
				{
					GameObject go = new GameObject("_Localization");
					DontDestroyOnLoad(go);
					mInstance = go.AddComponent<Localization>();
				}
			}
			return mInstance;
		}
	}

	/// <summary>
	/// Language the localization manager will start with.
	/// </summary>

	public string startingLanguage = "English";

	/// <summary>
	/// Available list of languages.
	/// </summary>

	public TextAsset[] languages;

	Dictionary<string, string> mDictionary = new Dictionary<string, string>();
	string mLanguage;

#if SHOW_REPORT
	BetterList<string> mUsed = new BetterList<string>();
#endif

	/// <summary>
	/// Name of the currently active language.
	/// </summary>

	public string currentLanguage
	{
		get
		{
			return mLanguage;
		}
		set
		{
			if (mLanguage != value)
			{
				startingLanguage = value;

				if (!string.IsNullOrEmpty(value))
				{
					// Check the referenced assets first
					if (languages != null)
					{
						for (int i = 0, imax = languages.Length; i < imax; ++i)
						{
							TextAsset asset = languages[i];

							if (asset != null && asset.name == value)
							{
								Load(asset);
								return;
							}
						}
					}

					// Not a referenced asset -- try to load it dynamically
					TextAsset txt = Resources.Load(value, typeof(TextAsset)) as TextAsset;

					if (txt != null)
					{
						Load(txt);
						return;
					}
				}

				// Either the language is null, or it wasn't found
				mDictionary.Clear();
				PlayerPrefs.DeleteKey("Language");
			}
		}
	}

	/// <summary>
	/// Determine the starting language.
	/// </summary>

	void Awake ()
	{
		if (mInstance == null)
		{
			mInstance = this;
			DontDestroyOnLoad(gameObject);

			currentLanguage = PlayerPrefs.GetString("Language", startingLanguage);

			if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
			{
				currentLanguage = languages[0].name;
			}
		}
		else Destroy(gameObject);
	}

	/// <summary>
	/// Oddly enough... sometimes if there is no OnEnable function in Localization, it can get the Awake call after UILocalize's OnEnable.
	/// </summary>

	void OnEnable () { if (mInstance == null) mInstance = this; }

#if SHOW_REPORT
	/// <summary>
	/// It's often useful to be able to tell which keys are used in localization, and which are not.
	/// For this to work properly it's advised to play through the entire game and view all localized content before hitting the Stop button.
	/// </summary>

	void OnDisable ()
	{
		string final = "";
		BetterList<string> full = new BetterList<string>();

		// Create a list of all the known keys
		foreach (KeyValuePair<string, string> pair in mDictionary) full.Add(pair.Key);

		// Sort the full list
		full.Sort(delegate(string s1, string s2) { return s1.CompareTo(s2); });

		// Create the final string with the localization keys
		for (int i = 0; i < full.size; ++i)
		{
			string key = full[i];
			string val = mDictionary[key].Replace("\n", "\\n");
			if (mUsed.Contains(key)) final += key + " = " + val + "\n";
			else final += "//" + key + " = " + val + "\n";
		}
		
		// Show the final report in a format that makes it easy to copy/paste into the original localization file
		if (!string.IsNullOrEmpty(final))
			Debug.Log("// Localization Report\n\n" + final);
	}
#endif

	/// <summary>
	/// Remove the instance reference.
	/// </summary>

	void OnDestroy () { if (mInstance == this) mInstance = null; }

	/// <summary>
	/// Load the specified asset and activate the localization.
	/// </summary>

	public void Load (TextAsset asset)
	{
		ByteReader reader = new ByteReader(asset);
		Set(asset.name, reader.ReadDictionary());
	}

	/// <summary>
	/// Load the specified asset and activate the localization.
	/// </summary>

	public void Set (string languageName, Dictionary<string, string> dictionary)
	{
#if SHOW_REPORT
		mUsed.Clear();
#endif
		mLanguage = languageName;
		PlayerPrefs.SetString("Language", mLanguage);
		mDictionary = dictionary;
		UIRoot.Broadcast("OnLocalize", this);
	}

	/// <summary>
	/// Localize the specified value.
	/// </summary>

	public string Get (string key)
	{
#if UNITY_EDITOR
		if (!Application.isPlaying) return key;
#endif
#if SHOW_REPORT
		if (!mUsed.Contains(key)) mUsed.Add(key);
#endif
		string val;
#if UNITY_IPHONE || UNITY_ANDROID
		if (mDictionary.TryGetValue(key + " Mobile", out val)) return val;
#endif

#if UNITY_EDITOR
		if (mDictionary.TryGetValue(key, out val)) return val;
		Debug.LogWarning("Localization key not found: '" + key + "'");
		return key;
#else
		return (mDictionary.TryGetValue(key, out val)) ? val : key;
#endif
	}

	/// <summary>
	/// Localize the specified value.
	/// </summary>

	static public string Localize (string key) { return (instance != null) ? instance.Get(key) : key; }
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Scripts/Internal/Localization.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000