Bulk Auto-Translation

More
9 years 7 months ago #186 by Jos
Bulk Auto-Translation was created by Jos
Was wondering if there is a way to trigger the translation for all keys in a language, rather then having to individually click the translate button? Even if this was an API-only thing, i could wire something up to do this.

Use case: We want to test with a language to get an idea of the size of the translated text, even if it isn't the "real" translations we will have done later on.

Thanks!
Jos

Please Log in or Create an account to join the conversation.

More
9 years 7 months ago #187 by Jos
Replied by Jos on topic Bulk Auto-Translation
I see that I2.GoogleTranslation has the method i want Translate(), but it is an editor class. Which just means that i need to write an editor script. Now i need a way to iterate though all the keys...

Please Log in or Create an account to join the conversation.

More
9 years 7 months ago #188 by Frank
Replied by Frank on topic Bulk Auto-Translation
Hi,

One way should be to export all the terms into a google spreadsheet and use the language functions to do the translation.

The other is to add a code like this into UpgradeManager.cs
        [MenuItem("Tools/I2 Localization/Translate All", false, 0)]
        public static void TranslateAll()
        {
            string TargetLanguage = "Spanish";
            if (!LocalizationEditorDB.HasLanguageSource(true))
            {
                Debug.Log("Can't find source");
                return;
            }

            LanguageSource Source = LocalizationEditorDB.mLanguageSource;
            int LanguageIndex = Source.GetLanguageIndex(TargetLanguage);
            if (LanguageIndex<0) 
            {
                Debug.Log("Can't find language in source");
                return;
            }

            for (int i = 0, imax = Source.mTerms.Count; i < imax; ++i)
            {
                TermData data = Source.mTerms[i];
                data.Languages[LanguageIndex] = GoogleTranslation.Translate(data.Languages[0], Source.mLanguages[0].Code, Source.mLanguages[LanguageIndex].Code);
            }
        }

I haven't test that code, but it should take the selected source, and translate from the first language into "Spanish".
You can change that language name at the top of the function.

Hope that helps. And I see that could be handy, so I will add that feature into the source editor. So that after the terms list there could be buttons to auto translate all the empty cells or just the selected languages.

Thanks,
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out

Please Log in or Create an account to join the conversation.

More
9 years 7 months ago #189 by Jos
Replied by Jos on topic Bulk Auto-Translation
Ha, I missed your reply while writing this EditorWindow code:
using UnityEngine;
using UnityEditor;
using System.Collections;

public class AutoTranslate : EditorWindow {


	
	[MenuItem("Window/Bulk Translate")]
	public static void Main()
	{
		EditorWindow.GetWindow( typeof( AutoTranslate ) );
	}

	private bool _translating = false;
	private int index = 0;
	private int termsTotal = 0;
	private I2.Loc.TermData term;
	private string trans = "";

	void OnGUI()
	{
		if(_translating)
		{
			DoTranslatingGUI();
		}
		else
		{
		
			if(GUILayout.Button("German"))
			{
				index = 0;
				termsTotal = I2.Loc.LocalizationEditorDB.mLanguageSource.mTerms.Count;
				_translating = true;
			}
		}
	}
	void DoTranslatingGUI()
	{
		GUILayout.Label("Translating Term: " + index + " of " + termsTotal);
		GUILayout.Label("-- " + trans);
		if(GUILayout.Button("Cancel"))
		{
			_translating = false;
		}
	}

	void Update()
	{
		if(index >= termsTotal && _translating)
		{
			_translating = false;
			Repaint ();
		}
		else
		{
			term = I2.Loc.LocalizationEditorDB.mLanguageSource.mTerms[index];
			trans = I2.GoogleTranslation.Translate(term.Languages[0], "en", "de");
			term.Languages[2] = trans;
			//Debug.Log("-- " + term.Term + "//" + term.Languages[0] + "//" + trans);
			index++;
			Repaint();
		}
	}

}

I'll take your less hard-coded bits and integrate with this one. I'm translating ~1200 terms, so i didn't want something that would lock the editor up.

Thanks for the super fast turn-around on that reply! :) Please also feel free to take any of this code if it is useful for you.

Jos

Please Log in or Create an account to join the conversation.

More
9 years 7 months ago #190 by Jos
Replied by Jos on topic Bulk Auto-Translation
Here is a slightly nicer version, with a checkbox for over-writing existing values. Also less hard-coded numbers.
using UnityEngine;
using UnityEditor;
using System.Collections;

public class AutoTranslate : EditorWindow {


	
	[MenuItem("Window/Bulk Translate")]
	public static void Main()
	{
		EditorWindow.GetWindow( typeof( AutoTranslate ) );
	}

	private bool _translating = false;
	private int index = 0;
	private int termsTotal = 0;
	private I2.Loc.TermData term;
	private string trans = "";
	private int langIndex = 0;

	private string[] langCodes = new string[]{"en", "fr", "de", "it", "es"};
	private string[] langNames = new string[]{"English", "French", "German", "Italian", "Spanish"};

	private bool overwriteExisting = false;

	void OnGUI()
	{
		if(_translating)
		{
			DoTranslatingGUI();
		}
		else
		{
			for(int i = 1; i < 5 ; i++)
			{
				
				if(GUILayout.Button(langNames[i]))
				{
					langIndex = i;
					index = 0;
					termsTotal = I2.Loc.LocalizationEditorDB.mLanguageSource.mTerms.Count;
					_translating = true;
				}
			}

			overwriteExisting = GUILayout.Toggle(overwriteExisting, "Overwrite Existing Translations");
		}
	}
	void DoTranslatingGUI()
	{
		GUILayout.Label("Translating Term: " + index + " of " + termsTotal);
		if(GUILayout.Button("Cancel"))
		{
			_translating = false;
		}
	}

	void Update()
	{
		if(index >= termsTotal && _translating)
		{
			_translating = false;
			Repaint ();
		}
		else if(_translating)
		{
			term = I2.Loc.LocalizationEditorDB.mLanguageSource.mTerms[index];
			if(string.IsNullOrEmpty(term.Languages[langIndex]) || overwriteExisting)
			{
				trans = I2.GoogleTranslation.Translate(term.Languages[0], "en", langCodes[langIndex]);
				term.Languages[langIndex] = trans;
			}
			index++;
			Repaint();
		}
	}

}

Please Log in or Create an account to join the conversation.

More
2 years 4 months ago #4612 by aloskjfna
Replied by aloskjfna on topic Bulk Auto-Translation
This forum has some stuff that talks about Bulk Auto-Translation so in case you think it is good enough and has some stuff that we will like to let others know then that would be a great help for those who need it for some reason at this year. I liked pendrago reviews now.

Please Log in or Create an account to join the conversation.

Time to create page: 0.169 seconds
Template by JoomlaShine