• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Translate Index References with Array

Engaged ,
Nov 06, 2020 Nov 06, 2020

Copy link to clipboard

Copied

Hello,

I have this script to translate the Index Reference from English to another languages, but the script stop before finish to translate all words, specialy in large books.

I need clic on Run de Script several times, in order to translate all index entries. 

 

Please, do you know why the script stopped continuosly?

 

The script:

 

var myTerms = [
    
    ['text','translation'],
    ['text text\(s\)','translation translation\(s\)'],
    ['text\’s','translation\’s'],
    ['text\, text','translation\, translation'],
    ['text\-text','translation\-translation'],
    ['text ö','translation ö'],
    ['text \& text','translation \& translation']

];
var doc = app.activeDocument;
for (var i=0; i<doc.indexes.length; i++) {  
        renameTopics(doc.indexes[i]);  
    } // for  
alert("Done!\n Sometimes with a large translations, is recomend run the script more than 3 times to be sure all words are translated.");
function renameTopics(myIndex)
{
var j;   
   for (var idx = 0; idx < myIndex.topics.length; idx++) {
    if( myTest(myIndex.topics[idx]) ) { renameTopics(myIndex.topics[idx]); }
       
       j=translateIt(myIndex.topics[idx].name+"", myTerms);
       if(j != null)
       {
           myIndex.topics[idx].name = j;
       }
   } //for    
}// fnc

function translateIt(myStr, myArr)
{
    for(k=0; k< myArr.length; k++)
    {
        if(myStr == myArr[k][0]) return myArr[k][1];
    }
return null;
}

function myTest(myNextLevel)
{
     var result;
     try {var myLen = myNextLevel.topics.length;
       result = true;
      } //try
        catch (er) {result = false}    
    return result;
}

 

 

 

I attach an InDesign document if you want to test. See here in action:

 

Screen Shot 2020-11-06 at 11.22.24 PM.pngScreen Shot 2020-11-06 at 11.22.39 PM.png

 

 

Like you can see don't translate all index entries:

Screen Shot 2020-11-06 at 11.22.14 PM.png

 

Thanks so much in advance!

 
 
 
 
TOPICS
Scripting

Views

211

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 06, 2020 Nov 06, 2020

The issue was that as soon as you were renaming the topic it was being sorted and hence its index in the collection was changed. So we were getting into a condition where we were missing entries in the loop as an already translated topic moved down in the collection after translation. Try the following modification of the method, here I am working on the state of the topic collection before the translation, so changes in the index(as described above) does not affect the outcome.

function renameT
...

Votes

Translate

Translate
Community Expert ,
Nov 06, 2020 Nov 06, 2020

Copy link to clipboard

Copied

The issue was that as soon as you were renaming the topic it was being sorted and hence its index in the collection was changed. So we were getting into a condition where we were missing entries in the loop as an already translated topic moved down in the collection after translation. Try the following modification of the method, here I am working on the state of the topic collection before the translation, so changes in the index(as described above) does not affect the outcome.

function renameTopics(myIndex)
{
	var j;
	var tp = myIndex.topics.everyItem().getElements()
	for (var idx = 0; idx < tp.length; idx++) {
		if( myTest(tp[idx]) ) {
			renameTopics(tp[idx]); 
		}
       
	   j=translateIt(tp[idx].name+"", myTerms);
	   if(j != null)
	   {
		   tp[idx].name = j;
	   }
   } //for    
}

-Manan

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 07, 2020 Nov 07, 2020

Copy link to clipboard

Copied

LATEST

Amazing!

I test in a large books close to 2000 entries, and works pretty nice.

 

Manan, you're very kind.

Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines