Dynamically Change Text Found In A Context Using A Script

Hi,

I have templates that have many headings for various sections of text in their print contexts. These headings need to be in multiple languages. Instead of adding many positioned boxes that are stacked on top of each other and make them conditional, I thought that a script that would read a field and based on that field change the text in said positioned boxes would be easier to manage.

Consider this pseudocode:

var languageCheck = record.fields.LanguageCheck;
//section.html is the name of my print context
var section = loadhtml(‘contexts/print/section.html’);

switch(languageCheck){
case “S”:
results.html(section.find(“@StatementDate@”).text(“STAATDATUM”));
break;
case “B”:
results.html(section.find(“@StatementDate@”).text(“Statement Date”));
}

I’m sure it can be done if I new the correct syntax.

Your help is much appreciated.

Regards,

S

Not sure what each of the heading will contain or whether they will contain several words to be translated but you could create each language heading in its own separate snippet file.

For example for Language “S”, have a snippet named S.html with all the words in that language

For language “B”, have another snippet named B.html with all the words in that language

etc…

Then use the following code to load the correct snippet:

results.loadhtml(‘snippets/’ + record.fields.LanguageCheck + ‘.html’)

If there are several words scattered all over the documents that needs to be translated depending on whether you are on a record which speaks language S or B then I would recommend creating a Section for each of the languages instead. For example, for language “B”, create a section named “Section B” and design the document section entirely in that language and do the same for other language. Of course, make use of shared snippets where possible.

Then using a control script, you can enable one section or the other depending on whether you are on a record which speaks that language:

var printSections = merge.template.contexts.PRINT.sections;

printSections[‘Section B’].enabled = false;
printSections[‘Section S’].enabled = false;

if(record.fields.LanguageCheck === ‘B’){
printSections[‘Section B’].enabled = true;
} else {
printSections[‘Section S’].enabled = true;
}

Hi Rod,

I’m not sure if I was clear enough in my op. Consider the below image that has 5 positioned boxes. I want to change the value in box 1 to either a English version of a word (or two) or say a German version of a word. A value in the data would be the deciding factor of what word to use. All boxes will either change to English versions of their respective words or German. I know this can be done using the option to convert the positioned box to a condition but the problem also arises that if you stack boxes on top of each other and want to adjust both you are not able to select multiple boxes at the same time and can in error only re-position one language box and not the one under it.

I do not want to use multiple print contexts because we print 99% of our work on pre-printed Litho stationary and at times the template would have to be adjusted for alignment. Having multiple contexts means more work/management.

PS. I wish that a future update would allow us to select multiple objects with Shift+LeftClick and CTRL+A or dragging a box around multiple objects in order to select them.

Thanks for the advise.

The problem with the approach you want to take is that it will dramatically slow down content creation. Remember that you want to minimize the number of scripts in the Designer and you also want to minimize the number of search and replace scripts that is done on a page. Your approach means the engine will read the template line by line to perform the translation and this will be done on every single page.

The multiple sections approach means the engines skips the translation process as it has already been done. This greatly improves Content Creation times especially if you are dealing with thousands of records. In addition, maintaining this document will be a lot easier as there won’t be too many scripts flying around. This approach also works well for printed output.

The multiple snippets approach can also improve content creation over the approach you are intending to take as translation is done in the snippets and therefore, there will be a single line of code to load the correct snippet for that language.

Note that you could also add those words you want to translate as fields in your data mapper and translate them there.

Finally, if you insist on translating the words in the Designer (for a reason I still don’t understand), you need to query the ‘body’ and then replace the text.

switch (record.fields.LanguageCheck) {
case ‘S’:
query(‘body’).find(‘@StatementDate@’).text(‘STAATDATUM’);
break;
case ‘B’:
query(‘body’).find(‘@StatementDate@’).text(‘Statement Date’);
break;
}

Thanks Rod for your explanations and insight. I would indeed rather have speed when it come to processing time. So can I then assume that 50 conditional scripts would be quicker than querying the body?

I guess it depends on the scripts. You can test this and see\compare the results yourself using the script Profiler available in the Connect Designer:

Click on Context in the Designer menu bar, and click on Profile Scripts. You can then compare the time it takes for each template to run each of the scripts only over 1000 iterations.

More information on the Profiler is available here:

http://help.objectiflune.com/EN/pres-connect-user-guide/1.4/#designer/Interface/Dialog_Profile_Scripts.htm?

I did not know about the Profiler. Thanks for yet another great tip. I’ll be sure to check it out. Thanks again.

Regards,

S