forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalizationDatabase.cs
More file actions
199 lines (181 loc) · 6.29 KB
/
Copy pathLocalizationDatabase.cs
File metadata and controls
199 lines (181 loc) · 6.29 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine.Internal;
using UnityEngine.Assertions;
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Security;
using System.Security.Cryptography;
namespace UnityEditor
{
[ExcludeFromDocs]
public class L10n
{
private L10n() {}
public static string Tr(string str)
{
var new_str = LocalizationDatabase.GetLocalizedString(str);
return new_str;
}
public static string[] Tr(string[] str_list)
{
var res = new string[str_list.Length];
for (var i = 0; i < res.Length; ++i)
res[i] = Tr(str_list[i]);
return res;
}
public static string Tr(string str, string groupName)
{
var new_str = LocalizationDatabase.GetLocalizedStringWithGroupName(str, groupName);
return new_str;
}
public static string TrPath(string path)
{
string[] separatingChars = { "/" };
var result = new System.Text.StringBuilder(256);
var items = path.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < items.Length; ++i)
{
result.Append(Tr(items[i]));
if (i < items.Length - 1)
result.Append("/");
}
return result.ToString();
}
}
internal static class LocalizationGroupStack
{
static Stack<string> s_GroupNameStack;
public static void Push(string groupName)
{
if (s_GroupNameStack == null)
s_GroupNameStack = new Stack<string>();
if (s_GroupNameStack.Count >= 16)
Assert.IsTrue(false); // check the leak.
s_GroupNameStack.Push(groupName);
LocalizationDatabase.SetContextGroupName(groupName);
}
public static void Pop()
{
if (s_GroupNameStack == null || s_GroupNameStack.Count <= 0)
Assert.IsTrue(false);
s_GroupNameStack.Pop();
if (s_GroupNameStack.Count > 0)
{
string top = s_GroupNameStack.Peek();
LocalizationDatabase.SetContextGroupName(top);
}
else
LocalizationDatabase.SetContextGroupName(null);
}
}
}
namespace UnityEditor.Localization.Editor
{
/// <summary>
/// This provides Localization function for Packages.
/// </summary>
public static class Localization
{
/// <summary>
/// get proper translation for the given argument.
/// </summary>
public static string Tr(string str)
{
if (!LocalizationDatabase.enableEditorLocalization)
return str;
var assembly = System.Reflection.Assembly.GetCallingAssembly();
object[] attrobjs = assembly.GetCustomAttributes(typeof(LocalizationAttribute), true /* inherit */);
if (attrobjs != null && attrobjs.Length > 0 && attrobjs[0] != null)
{
LocalizationAttribute locAttr = (LocalizationAttribute)attrobjs[0];
string locGroupName = locAttr.locGroupName;
if (locGroupName == null)
locGroupName = assembly.GetName().Name;
var new_str = LocalizationDatabase.GetLocalizedStringWithGroupName(str, locGroupName);
return new_str;
}
return str;
}
}
/// <summary>
/// This provides an auto dispose Localization system.
/// This can be called recursively.
/// </summary>
public class LocalizationGroup : IDisposable
{
string m_LocGroupName;
bool m_Pushed = false;
/// <summary>
/// a current group name for the localization.
/// </summary>
public string locGroupName { get { return m_LocGroupName; } }
/// <summary>
/// Default constructor.
/// </summary>
public LocalizationGroup()
{
var assembly = System.Reflection.Assembly.GetCallingAssembly();
initialize(assembly);
}
/// <summary>
/// constructor.
/// <param name="behaviour">group name will become the name of Assembly the behaviour belongs to.</param>
/// </summary>
public LocalizationGroup(Behaviour behaviour)
{
if (behaviour != null)
{
System.Type type = behaviour.GetType();
initialize(type.Assembly);
}
}
/// <summary>
/// constructor.
/// <param name="type">group name will become the name of Assembly the type belongs to.</param>
/// </summary>
public LocalizationGroup(System.Type type)
{
initialize(type.Assembly);
}
/// <summary>
/// constructor.
/// <param name="obj">group name will become the name of Assembly the obj belongs to.</param>
/// </summary>
public LocalizationGroup(System.Object obj)
{
if (obj == null)
return;
initialize(obj.GetType().Assembly);
}
void initialize(System.Reflection.Assembly assembly)
{
string groupName = null;
object[] attrobjs = assembly.GetCustomAttributes(typeof(LocalizationAttribute), true /* inherit */);
if (attrobjs != null && attrobjs.Length > 0 && attrobjs[0] != null) // focus on only the first.
{
LocalizationAttribute locAttr = (LocalizationAttribute)attrobjs[0];
groupName = locAttr.locGroupName;
if (groupName == null)
groupName = assembly.GetName().Name;
}
LocalizationGroupStack.Push(groupName);
m_Pushed = true;
m_LocGroupName = groupName;
}
/// <summary>
/// dispose current state.
/// </summary>
public void Dispose()
{
if (m_Pushed)
LocalizationGroupStack.Pop();
}
}
}