Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Problem

When working in a multilingual environment not all objects are automatically translated. For example, when you are working with a model package with languages English and Dutch. When objects are added when working in the English language, these same objects will turn up blank when the Dutch language is used.


Solution

There is a script available that will copy the translations of the default language to the other languages. Since it adds translations and in essence changes your model package, it is recommended that you create a backup before running the script.

  1. Set the scoping of the script by selecting one or more objects or a folder containing objects. If you select objects (in the view or in the model browser), the translations of these object will be synchronized. If you select a folder (in the model browser), all objects within that folder will be synchronized.

    Tip

    For test purposes it is recommended to start with one object or a folder that contains only a few objects.


  2. Check if the correct default language is set, and change it if needed via the Translation Table on the Advanced ribbon tab. The default language will be used as a starting point.

  3. Run the script below using the script editor. At the end of the script a message window will appear with a summary of the results.


Code Block
// This script copies string and rtf property values in the default language to all other languages that do not yet have a value set.

scope = undefined;
if (selection.empty()) {
	messageBox("Please select the objects you want to copy texts for.");
	stop;
}
forall obj in selection {
	if (scope.isUndefined()) {
		scope = Set();
	}
	if (obj is "Reference") {
		scope.add(obj.to);
	}
	else {
		scope.add(obj);
	}
}
if (scope.isUndefined()) {
	messageBox("No appropriate objects were selected.");
	stop;
}

//this gives us the default language and a list of languages used in the model package;
LangList = List();
DefaultLang = "";
forall "MM_Language" lang in modelpackage {
	if (lang.isDefault == true) {
		DefaultLang = lang.nm;
	}
	else {
		LangList.add(lang.nm);
	}
}

if (DefaultLang.empty()) {
	error "No default language specified";
	stop;
}

// Isolate roots
while (true) {
	foundChild = false;
	forall x in scope {
		forall y in scope {
			if (y != x) {
				if (y.parentPath().contains(x)) {
					scope.remove(y);
					foundChild = true;
					break;
				}
			}
		}
		if (foundChild) {
			break;
		}
	}
	if (!foundChild) {
		break;
	}
}

message "*** Start search for translations to copy...";
message "Default language: " + DefaultLang;
message "Scope: " + scope;

//keep a counter of the changes per language
forall root in scope {
	forall obj in root {
		if (obj is "Reference") {
			continue;
		}
		changedAttrs = Index();		// log
		forall atr in obj.attrsRef() {
			if (obj.hasUserValueRef(atr, DefaultLang)) {
				AtrDefaultLangVal = obj.attrValueRef(atr, DefaultLang);
				if (AtrDefaultLangVal.isString() || AtrDefaultLangVal.isRtf()) {
					//only translate strings/rtfs
					changedLangs = changedAttrs.valueFor(atr.name());
					forall lang in LangList {
						if (lang != DefaultLang) {
							//if nothing has been filled in:
							if (!obj.hasUserValueRef(atr, lang)) {
								//try to set the translated value to the same value as the DefaultLang
								obj.setAttrValue(atr, AtrDefaultLangVal, lang);

								//increment the change count for this language
								if (changedLangs.isUndefined()) {
									changedLangs = List();
								}
								changedLangs.add(lang);
							}
						}
					}
					if (!changedLangs.isUndefined()) {
						changedAttrs.add(atr.name(), changedLangs.sorted());
					}
				}
			}
		}
		if (!changedAttrs.empty()) {
			msg = format("%s '%s': ", obj.type(), obj.name());
			first = true;
			forall a, l in changedAttrs {
				if (first) {
					first = false;
				}
				else {
					msg.add(", ");
				}
				msg.add("'" + translate(a) + "' set for languages [" + l + "]");
			}
			message msg, obj;
		}
	}
}
message "*** End of search.";




Content by Label
showLabelsfalse
max6
showSpacefalse
titleRelated articles
cqllabel = "translation" AND label not in ("troubleshooting","archive") and space = "knowledge"