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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
using UnityEngine;
using System.Collections.Generic;
using Lean.Common;
#if UNITY_EDITOR
using UnityEditor;
namespace Lean.Localization.Examples
{
[CanEditMultipleObjects]
[CustomEditor(typeof(LeanLanguageCSV), true)]
public class LeanLanguageCSV_Inspector : LeanInspector<LeanLanguageCSV>
{
protected override void DrawInspector()
{
Draw("Source", "The text asset that contains all the translations.");
Draw("Language", "The language of the translations in the source file.");
Draw("Separator", "The string separating the phrase name from the translation.");
Draw("NewLine", "The string denoting a new line within a translation.");
Draw("Comment", "The (optional) string separating the translation from the comment (empty = no comments).");
EditorGUILayout.Separator();
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.LabelField("CollectItem" + Target.Separator + "アイテム" + Target.NewLine + "集めました" + Target.Comment + "Comment here");
EditorGUI.EndDisabledGroup();
EditorGUILayout.Separator();
EditorGUILayout.BeginHorizontal();
if (Any(t => t.Entries.Count > 0))
{
if (GUILayout.Button("Clear") == true)
{
Each(t => t.Clear());
}
}
if (GUILayout.Button("Load Now") == true)
{
Each(t => t.LoadFromSource());
}
if (GUILayout.Button("Export") == true)
{
Each(t => t.ExportTextAsset());
}
EditorGUILayout.EndHorizontal();
if (Targets.Length == 1)
{
var entries = Target.Entries;
if (entries.Count > 0)
{
EditorGUILayout.Separator();
EditorGUI.BeginDisabledGroup(true);
foreach (var entry in entries)
{
EditorGUILayout.TextField(entry.Name, entry.Text);
}
EditorGUI.EndDisabledGroup();
}
}
}
}
}
#endif
namespace Lean.Localization
{
/// <summary>This component will load localizations from a CSV file. By default they should be in the format:
/// Phrase Name Here = Translation Here // Optional Comment Here</summary>
[ExecuteInEditMode]
[HelpURL(LeanLocalization.HelpUrlPrefix + "LeanLanguageCSV")]
[AddComponentMenu(LeanLocalization.ComponentPathPrefix + "Language CSV")]
public class LeanLanguageCSV : LeanSource
{
[System.Serializable]
public class Entry
{
public string Name;
public string Text;
}
/// <summary>The text asset that contains all the translations.</summary>
public TextAsset Source;
/// <summary>The language of the translations in the source file.</summary>
[LeanLanguageName]
public string Language;
/// <summary>The string separating the phrase name from the translation.</summary>
public string Separator = " = ";
/// <summary>The string denoting a new line within a translation.</summary>
public string NewLine = "\\n";
/// <summary>The (optional) string separating the translation from the comment (empty = no comments).</summary>
public string Comment = " // ";
/// <summary>This stores all currently loaded translations from this CSV file.</summary>
public List<Entry> Entries { get { if (entries == null) entries = new List<Entry>(); return entries; } } [SerializeField] private List<Entry> entries;
/// <summary>The characters used to separate each translation.</summary>
private static readonly char[] newlineCharacters = new char[] { '\r', '\n' };
private static Stack<Entry> entryPool = new Stack<Entry>();
public override void Compile(string primaryLanguage, string secondaryLanguage)
{
if (entries == null || entries.Count == 0)
{
if (Application.isPlaying == true)
{
LoadFromSource();
}
}
if (entries != null)
{
for (var i = entries.Count - 1; i >= 0; i--)
{
var entry = entries[i];
var translation = LeanLocalization.RegisterTranslation(entry.Name);
translation.Register(Language, this);
if (Language == primaryLanguage)
{
translation.Data = entry.Text;
translation.Primary = true;
}
else if (Language == secondaryLanguage && translation.Primary == false)
{
translation.Data = entry.Text;
}
}
}
}
[ContextMenu("Clear")]
public void Clear()
{
if (entries != null)
{
entries.Clear();
// Update translations?
if (LeanLocalization.CurrentLanguage == Language)
{
LeanLocalization.UpdateTranslations();
}
}
}
[ContextMenu("Load From Source")]
public void LoadFromSource()
{
if (Source != null && string.IsNullOrEmpty(Language) == false)
{
for (var i = Entries.Count - 1; i >= 0; i--) // NOTE: Property
{
entryPool.Push(entries[i]);
}
entries.Clear();
// Split file into lines, and loop through them all
var lines = Source.text.Split(newlineCharacters, System.StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
var equalsIndex = line.IndexOf(Separator);
// Only consider lines with the Separator character
if (equalsIndex != -1)
{
var name = line.Substring(0, equalsIndex).Trim();
var text = line.Substring(equalsIndex + Separator.Length).Trim();
// Does this entry have a comment?
if (string.IsNullOrEmpty(Comment) == false)
{
var commentIndex = text.LastIndexOf(Comment);
if (commentIndex != -1)
{
text = text.Substring(0, commentIndex).Trim();
}
}
// Replace newline markers with actual newlines
if (string.IsNullOrEmpty(NewLine) == false)
{
text = text.Replace(NewLine, System.Environment.NewLine);
}
var entry = entryPool.Count > 0 ? entryPool.Pop() : new Entry();
entry.Name = name;
entry.Text = text;
entries.Add(entry);
}
}
// Update translations?
if (LeanLocalization.CurrentLanguage == Language)
{
LeanLocalization.UpdateTranslations();
}
}
}
#if UNITY_EDITOR
[ContextMenu("Export Text Asset")]
public void ExportTextAsset()
{
if (string.IsNullOrEmpty(Language) == false)
{
// Find where we want to save the file
var path = EditorUtility.SaveFilePanelInProject("Export Text Asset for " + Language, Language, "txt", "");
// Make sure we didn't cancel the panel
if (string.IsNullOrEmpty(path) == false)
{
if (LeanLocalization.CurrentLanguage == Language)
{
DoExportTextAsset(path);
}
else
{
var oldLanguage = LeanLocalization.CurrentLanguage;
LeanLocalization.CurrentLanguage = Language;
DoExportTextAsset(path);
LeanLocalization.CurrentLanguage = oldLanguage;
}
}
}
}
private void DoExportTextAsset(string path)
{
var data = "";
var gaps = false;
// Add all phrase names and existing translations to lines
foreach (var pair in LeanLocalization.CurrentTranslations)
{
var translation = pair.Value;
if (gaps == true)
{
data += System.Environment.NewLine;
}
data += pair.Key + Separator;
gaps = true;
if (translation.Data is string)
{
var text = (string)translation.Data;
// Replace all new line permutations with the new line token
text = text.Replace("\r\n", NewLine);
text = text.Replace("\n\r", NewLine);
text = text.Replace("\n", NewLine);
text = text.Replace("\r", NewLine);
data += text;
}
}
// Write text to file
using (var file = System.IO.File.OpenWrite(path))
{
var encoding = new System.Text.UTF8Encoding();
var bytes = encoding.GetBytes(data);
file.Write(bytes, 0, bytes.Length);
}
// Import asset into project
AssetDatabase.ImportAsset(path);
// Replace Soure with new Text Asset?
var textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset));
if (textAsset != null)
{
Source = textAsset;
EditorGUIUtility.PingObject(textAsset);
EditorUtility.SetDirty(this);
}
}
#endif
}
}
|