Skip to content

Commit

Permalink
Version 0.1.0 - First working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksymHernets committed Feb 13, 2022
1 parent 558acfa commit 2e63af0
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 114 deletions.
Binary file not shown.
21 changes: 21 additions & 0 deletions Assets/Editor/AutoTranslate.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "AutoTranslate",
"rootNamespace": "",
"references": [
"GUID:9e24947de15b9834991c9d8411ea37cf",
"GUID:eec0964c48f6f4e40bc3ec2257ccf8c5",
"GUID:84651a3751eca9349aac36a66bba901b",
"GUID:73f7137f823496d4180a1d859c2fcabe"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Assets/Editor/AutoTranslate.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

193 changes: 117 additions & 76 deletions Assets/Editor/WindowAutoTranslate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using GoodTime.Tools.GoogleApiTranslate;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
Expand All @@ -12,15 +14,17 @@ namespace GoodTime.HernetsMaksym.AutoTranslate.Windows
{
public class WindowAutoTranslate : EditorWindow
{
// Window parameters
private const string k_WindowTitle = "Auto Translate for Localization";
static readonly Vector2 k_MinSize = new Vector2(900, 600);

private LocalizationSettings _localizationSettings;
private TypeStage _typeStage;
private List<Locale> _locales;

private string _selectedLocale = "English";
// Temp
private List<Locale> _locales;
private Locale _selectedLocale;
private IList<StringTable> _tables;

// Arguments for translate
private string _selectedLanguage = string.Empty;
private bool _isOverrideWords = true;
private bool _isTranslateEmptyWords = true;
private bool _isTranslateSmartWords = true;
Expand All @@ -37,63 +41,14 @@ public void OnEnable()
{
_typeStage = TypeStage.Loading;

this.minSize = k_MinSize;

LoadSettings();

if (_locales.Count != 0)
{
_selectedLocale = _locales[0].name;
}
}

private void OnFocus()
{
LoadSettings();
}

private async void LoadSettings()
{
string[] guids = AssetDatabase.FindAssets("Localization Settings t:LocalizationSettings", null);

if ( guids.Length != 0 )
{
var path = AssetDatabase.GUIDToAssetPath(guids[0]);

_localizationSettings = AssetDatabase.LoadAssetAtPath<LocalizationSettings>(path);

_locales = _localizationSettings.GetAvailableLocales().Locales;

Locale selectedLocale = await _localizationSettings.GetSelectedLocaleAsync().Task;

if (selectedLocale != null)
{
_selectedLocale = selectedLocale.name;
}

_typeStage = TypeStage.Ready;
}
}

private async void LoadLaunchers()
{
_locales = new List<Locale>();

List<string> labels = new List<string>();
labels.Add("Locale");
IList<IResourceLocation> locations = await Addressables.LoadResourceLocationsAsync(labels, Addressables.MergeMode.None, typeof(Locale)).Task;

foreach (var location in locations)
{
_locales.Add(await Addressables.LoadAssetAsync<Locale>(location).Task);
}

// It is no work. Why?. I dont know...
//IList<Locale> lists = await Addressables.LoadAssetsAsync<Locale>(locations, null, Addressables.MergeMode.Union).Task;

_typeStage = TypeStage.Ready;
}

void OnGUI()
{
if (_typeStage == TypeStage.Loading)
Expand All @@ -104,19 +59,19 @@ void OnGUI()
{
GUILayout.Space(10);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Source Launcher", GUILayout.Width(300));
EditorGUILayout.LabelField("Source language", GUILayout.Width(300));

var posit = new Rect(new Vector2(210, 10), new Vector2(200, 20));
if (EditorGUILayout.DropdownButton(new GUIContent(_selectedLocale), FocusType.Passive))
var posit = new Rect(new Vector2(310, 10), new Vector2(200, 20));
if (EditorGUILayout.DropdownButton(new GUIContent(_selectedLanguage), FocusType.Passive))
{
var genericMenu = new GenericMenu();

foreach (var option in _locales.Select(w => w.name))
{
bool selected = option == _selectedLocale;
bool selected = option == _selectedLanguage;
genericMenu.AddItem(new GUIContent(option), selected, () =>
{
_selectedLocale = option;
_selectedLanguage = option;
});
}
genericMenu.DropDown(posit);
Expand All @@ -134,44 +89,128 @@ void OnGUI()
_isTranslateSmartWords = EditorGUILayout.Toggle("Translate smart words", _isTranslateSmartWords);

GUILayout.Space(20);
if (GUILayout.Button("Translate") )
if (GUILayout.Button("Translate"))
{
Translate();
ButtonTranslate_Click();
}
}
else if (_typeStage == TypeStage.Translating)
{
EditorGUILayout.LabelField("Translating", EditorStyles.boldLabel);
}
else if (_typeStage == TypeStage.ErrorNoFoundSettings)
{
EditorGUILayout.LabelField("Error. Localization settings not found!", EditorStyles.boldLabel);
}
else if (_typeStage == TypeStage.ErrorNoFoundLocales)
{
EditorGUILayout.LabelField("Error. No languages found!", EditorStyles.boldLabel);
}
}

private void Translate()
private void LoadSettings()
{
_typeStage = TypeStage.Translating;
LoadTables();
string[] guids = AssetDatabase.FindAssets("Localization Settings t:LocalizationSettings", null);

if (guids.Length != 0)
{
var path = AssetDatabase.GUIDToAssetPath(guids[0]);

LocalizationSettings localizationSettings = AssetDatabase.LoadAssetAtPath<LocalizationSettings>(path);

_locales = localizationSettings.GetAvailableLocales().Locales;

if ( _locales == null || _locales.Count == 0)
{
_typeStage = TypeStage.ErrorNoFoundLocales;
return;
}

Locale selectedLocale = localizationSettings.GetSelectedLocale();

if ( selectedLocale != null )
{
_selectedLanguage = selectedLocale.LocaleName;
}

_selectedLanguage = _locales[0].LocaleName;

_typeStage = TypeStage.Ready;
}
else
{
_typeStage = TypeStage.ErrorNoFoundSettings;
}
}

public async void LoadTables()
private void ButtonTranslate_Click()
{
List<string> labels = new List<string>();
_typeStage = TypeStage.Translating;

Addressables.ClearResourceLocators();
Addressables.CleanBundleCache();

foreach (var item in _locales)
LoadTables().ContinueWith( _ =>
{
labels.Add("Locale-" + item.Formatter);
TranslateTables();

_typeStage = TypeStage.Ready;
}
, System.Threading.Tasks.TaskContinuationOptions.ExecuteSynchronously);
}

private async Task LoadTables()
{
List<string> labels = _locales.Select(w=> "Locale-" + w.Formatter).ToList();

IList<IResourceLocation> locations = await Addressables.LoadResourceLocationsAsync(labels, Addressables.MergeMode.Union, typeof(StringTable)).Task;

var tables = await Addressables.LoadAssetsAsync<StringTable>(locations, null).Task;
_tables = await Addressables.LoadAssetsAsync<StringTable>(locations, null).Task;

foreach (var table in tables)
{
//table["SampleText"].Value = "sample";
return;
}

table.AddEntry("SampleText", "sample4");
}
private void TranslateTables()
{
if ( _tables == null ) return;

_selectedLocale = _locales.First(w => w.LocaleName == _selectedLanguage);

_typeStage = TypeStage.Ready;
StringTable sourceLanguageTable = _tables.First(w => w.LocaleIdentifier == _selectedLocale.Identifier);

IList<StringTable> tablesForTranslate = _tables;
tablesForTranslate.Remove(sourceLanguageTable);

GoogleApiTranslate translator = new GoogleApiTranslate();

foreach (StringTable targetLanguageTable in tablesForTranslate)
{
foreach (var entry in sourceLanguageTable.SharedData.Entries)
{
StringTableEntry sourceWord = sourceLanguageTable.GetEntry(entry.Key);
if ( sourceWord.IsSmart == true && _isTranslateSmartWords == false)
{
continue;
}
StringTableEntry targetWord = targetLanguageTable.GetEntry(entry.Key);
if ( targetWord == null || string.IsNullOrEmpty(targetWord.Value) )
{
if ( _isTranslateEmptyWords == false)
{
continue;
}
}
else
{
if ( _isOverrideWords == false )
{
continue;
}
}
string result = translator.Translate(sourceWord.Value, sourceLanguageTable.LocaleIdentifier.Code, targetLanguageTable.LocaleIdentifier.Code);
targetLanguageTable.AddEntry(entry.Key, result);
}
}
}
}

Expand All @@ -180,7 +219,9 @@ public enum TypeStage
Loading,
Ready,
Translating,
Done
Done,
ErrorNoFoundSettings,
ErrorNoFoundLocales
}
}

2 changes: 1 addition & 1 deletion Assets/Samples/Scenes/Tables/SampleTable_en.asset
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MonoBehaviour:
m_Items: []
m_TableData:
- m_Id: 372829654384640
m_Localized: sample2
m_Localized: sample
m_Metadata:
m_Items: []
references:
Expand Down
14 changes: 14 additions & 0 deletions Assets/Scripts/GoogleApiTranslate.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "GoogleApiTranslate",
"rootNamespace": "GoodTime.Tools.GoogleApiTranslate",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Assets/Scripts/GoogleApiTranslate.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e63af0

Please sign in to comment.