Skip to content

Commit 91a05c8

Browse files
committed
improve samples
1 parent 9531d02 commit 91a05c8

11 files changed

Lines changed: 272 additions & 17 deletions

Editor/GptLocalization.asmdef

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "GptLocalization",
3+
"rootNamespace": "",
4+
"references": [
5+
"Unity.Localization",
6+
"Unity.Localization.Editor"
7+
],
8+
"includePlatforms": [
9+
"Editor"
10+
],
11+
"excludePlatforms": [],
12+
"allowUnsafeCode": false,
13+
"overrideReferences": false,
14+
"precompiledReferences": [],
15+
"autoReferenced": true,
16+
"defineConstraints": [],
17+
"versionDefines": [],
18+
"noEngineReferences": false
19+
}

Editor/GptLocalization.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/LocalizeGptWindow/LocalizeGptWindow.GUI.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public partial class LocalizeGptWindow
99
{
1010
enum OutputType
1111
{
12-
None, Prompt, Error,
12+
None, Prompt, Error, Info
1313
}
1414

1515
private SimpleEditorTableView<TranslateRec> _tableView;
@@ -155,14 +155,25 @@ private void OnGptModelGUI()
155155
if (_gptFoldout)
156156
{
157157
EditorGUI.indentLevel++;
158-
EditorGUI.BeginChangeCheck();
159158
EditorGUILayout.BeginVertical("Box");
160159
_baseUrl = EditorGUILayout.TextField("Base URL", _baseUrl);
161160
if (string.IsNullOrEmpty(_baseUrl))
162161
_baseUrl = DEFAULT_BASE_URL;
163162

164163
_apiKey = EditorGUILayout.PasswordField("API Key", _apiKey);
165-
164+
if (!IsValidOpenAIKey(_apiKey))
165+
{
166+
GUIStyle style = new GUIStyle(EditorStyles.helpBox)
167+
{
168+
richText = true
169+
};
170+
EditorGUILayout.BeginHorizontal();
171+
EditorGUI.indentLevel--;
172+
GUILayout.Space(EditorGUIUtility.labelWidth);
173+
EditorGUILayout.LabelField("<color=#ff4444>Please enter a valid OpenAI API Key.</color>", style);
174+
EditorGUI.indentLevel++;
175+
EditorGUILayout.EndHorizontal();
176+
}
166177
int index = Array.IndexOf(_validModels, _model);
167178
index = EditorGUILayout.Popup("Model", index, _validModels);
168179
if (index >= 0 && index < _validModels.Length)
@@ -224,7 +235,14 @@ private void OnOutputGUI()
224235
if (!string.IsNullOrEmpty(_outputStr) && _outputType != OutputType.None)
225236
{
226237
EditorGUILayout.Space();
238+
EditorGUILayout.BeginHorizontal();
227239
EditorGUILayout.LabelField(_outputType.ToString(), EditorStyles.boldLabel);
240+
if (GUILayout.Button("Clear", GUILayout.Width(100)))
241+
{
242+
_outputStr = string.Empty;
243+
_outputType = OutputType.None;
244+
}
245+
EditorGUILayout.EndHorizontal();
228246
EditorGUILayout.Space();
229247
if (_outputType == OutputType.Error)
230248
{
@@ -286,6 +304,12 @@ private void OnEntryListGUI()
286304
_tableView ??= CreateTable();
287305
_tableView.DrawTableGUI(_recs, (_recs.Length + 2) * EditorGUIUtility.singleLineHeight);
288306
}
289-
307+
308+
309+
private bool IsValidOpenAIKey(string key)
310+
{
311+
var apiKeyPattern = @"^sk-\w{32,}$";
312+
return System.Text.RegularExpressions.Regex.IsMatch(key, apiKeyPattern);
313+
}
290314
}
291315
}

Editor/LocalizeGptWindow/LocalizeGptWindow.Progress.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ private void UpdateTaskProgress()
5959
if (_pendingRecs != null && _currentPendingRecIndex < _pendingRecs.Length)
6060
{
6161
var rec = _pendingRecs[_currentPendingRecIndex];
62-
OnTaskCompleted(_task, rec);
62+
if (!OnTaskCompleted(_task, rec))
63+
{
64+
CancelTask();
65+
return;
66+
}
6367
_taskDuration = Time.realtimeSinceStartup - _taskStartTime;
6468

6569
if (_currentPendingRecIndex < _pendingRecs.Length - 1)
@@ -69,9 +73,12 @@ private void UpdateTaskProgress()
6973
_taskStartTime = Time.realtimeSinceStartup;
7074
} else
7175
{
76+
Output("Translation completed:\n" +
77+
string.Join("\n", _pendingRecs.Select((rec) => rec.key)), OutputType.Info);
7278
_task = null;
7379
_pendingRecs = null;
7480
RefreshRecords();
81+
7582
}
7683
}
7784
}

Editor/LocalizeGptWindow/LocalizeGptWindow.Translate.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ private TranslateRec GeneratePrompt(StringTableCollection collection, string key
3737
key = key, selected = true
3838
};
3939

40-
sb.Append("Translate the following texts:\n");
4140
var tables = collection.StringTables;
4241
rec.srcTables = new List<StringTable>();
4342
rec.dstTables = new List<StringTable>();
@@ -50,12 +49,15 @@ private TranslateRec GeneratePrompt(StringTableCollection collection, string key
5049
rec.dstTables.Add(table);
5150
continue;
5251
}
53-
54-
string langName = table.LocaleIdentifier.CultureInfo.EnglishName;
52+
5553
rec.srcTables.Add(table);
56-
if (rec.srcTables.Count <= 2)
54+
if (rec.srcTables.Count <= 1)
5755
{
58-
sb.Append("## From " + langName + ":\n" + entry.Value + "\n");
56+
sb.Append("Translate from ");
57+
sb.Append(table.LocaleIdentifier.CultureInfo.EnglishName);
58+
sb.Append(":\n");
59+
sb.Append(entry.Value);
60+
sb.Append("\n");
5961
}
6062
}
6163

@@ -128,24 +130,26 @@ private void AskGpt(TranslateRec rec)
128130
});
129131
}
130132

131-
private void OnTaskCompleted(Task<CreateChatCompletionResponse> task, TranslateRec rec)
133+
private bool OnTaskCompleted(Task<CreateChatCompletionResponse> task, TranslateRec rec)
132134
{
133135
if (task.IsFaulted)
134136
{
135137
if (task.Exception != null)
136138
{
137139
Output(task.Exception.Message, OutputType.Error);
138140
Debug.LogError(task.Exception);
141+
} else
142+
{
143+
Output("Task Unknown Error", OutputType.Error);
139144
}
140-
return;
145+
return false;
141146
}
142147

143148
var response = task.Result;
144149
if (response.Error != null)
145150
{
146151
Output(response.Error.Message, OutputType.Error);
147-
Debug.LogError(response.Error.Message);
148-
return;
152+
return false;
149153
}
150154

151155
string answer = response.Choices[0].Message.Content;
@@ -159,10 +163,12 @@ private void OnTaskCompleted(Task<CreateChatCompletionResponse> task, TranslateR
159163
} else
160164
{
161165
Output("Failed to parse response as json object:\n" + answer, OutputType.Error);
162-
Debug.LogError("Failed to parse response as json object");
166+
return false;
163167
}
164168

165-
Output("", OutputType.None);
169+
//Output(answer, OutputType.Info);
170+
171+
return true;
166172
}
167173

168174
private string TryParseText(JToken json)

Editor/LocalizeGptWindow/LocalizeGptWindow.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ private void Output(string str, OutputType type)
5858
{
5959
_outputStr = str;
6060
_outputType = type;
61+
if (type == OutputType.Error)
62+
Debug.LogError(str);
6163
}
6264

6365
private void UpdateFrame()

Intro.gif

1.23 MB
Loading

Intro.gif.meta

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
## Unity GPT Localization
2-
A Unity tool combining Localization and ChatGPT for easy multilingual translation using OpenAI API. Control translation quality with Localization comments.
2+
A Unity tool combining Localization and ChatGPT for easy multilingual translation using OpenAI API.
3+
4+
Control translation quality with Localization comments.
35

46
### Features
7+
1.
8+
2. **Localization Comments** - Add comments to your localization keys to control translation quality.
9+
2. **ChatGPT Translation** - Use OpenAI's ChatGPT to translate your keys to multiple languages.
10+
3. **Custom Translation** - Use your own translation service to translate keys.
11+
4. **Localization Manager** - A simple manager to handle translations in your game.
12+
5. **Localization Editor** - An editor to manage your localization keys and translations.
13+
514

615
### Installation
716

0 commit comments

Comments
 (0)