Skip to content

Commit

Permalink
translation example script
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewg42 committed Apr 14, 2010
1 parent f5df90f commit 9db1df8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions scripts/save_state.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function saveState(stateName)
savedStates[stateName]["RaAngle"] = core.getViewRaAngle();
savedStates[stateName]["DecAngle"] = core.getViewDecAngle();

savedStates[stateName]["AppLanguage"] = core.getAppLanguage();
savedStates[stateName]["SkyLanguage"] = core.getSkyLanguage();

core.debug("saveState() - state saved with ID: " + stateName);
}

Expand Down Expand Up @@ -168,6 +171,8 @@ function restoreState(stateName, options)
core.setFlagGravityLabels(savedStates[stateName]["FlagGravityLabels"]);
core.setDiskViewport(savedStates[stateName]["DiskViewport"]);
core.setObserverLocation(savedStates[stateName]["ObserverLocation"]);
core.setAppLanguage(savedStates[stateName]["AppLanguage"]);
core.setSkyLanguage(savedStates[stateName]["SkyLanguage"]);

// optional restoration options...
if (!options["time"])
Expand Down
26 changes: 26 additions & 0 deletions scripts/tests/translation.ssc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Name: Example of translations
// License: Public Domain
// Author: Matthew Gates
// Description: Illustration of using translation.inc
//

include ("../translation.inc");

setTr("de", "Hello world", "Hallo Welt");
setTr("de", "Knife", "das Messer");

dumpTr();

messages = new Array("Hello world", "Bananas are marsupials", "Knife", "Pra-Bob");

originalLanguage = core.getAppLanguage();

core.setAppLanguage("de");
for(i=0; i<messages.length; i++)
{
core.debug("\"" + messages[i] + "\" in " + core.getAppLanguage() + " is \"" + tr(messages[i]) + "\"");
}

core.setAppLanguage(originalLanguage);

65 changes: 65 additions & 0 deletions scripts/translation.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Name: Provide simple translation option for scripts
// License: Public Domain
// Author: Matthew Gates
// Description: Simple translation functions for scripts.
// set translations with the setTr function, then use tr(string)
// everywhere in your script where you want to get a translated
// string. The current application language is taken from the
// Application Language setting. See core.setAppLanguage and
// core.getAppLanguage for details.
//

// declare a global variable to store saved state
var translationStrings = new Array();

// call this function with some string ID
function tr(str)
{
lang = core.getAppLanguage();
// core.debug("tr: getting " + lang + " translation for " + str);
if (translationStrings[lang]==undefined)
{
// core.debug("tr: no lang array");
return str;
}
else if (translationStrings[lang][str]==undefined)
{
// core.debug("tr: no phrase array");
return str;
}
else
{
// core.debug("tr: got it");
return translationStrings[lang][str];
}
}

function setTr(lang, original, translation)
{
// core.debug("setTr: " + lang + ", " + original + ", " + translation);
if (translationStrings[lang]==undefined)
{
// core.debug("setTr: making new lang array");
translationStrings[lang] = new Array();
}

translationStrings[lang][original] = translation;

}

function dumpTr()
{
core.debug("We have the following translations:");
for (lang in translationStrings)
{
core.debug("Language: " + lang);
strings = translationStrings[lang];
for (phrase in strings)
{
core.debug(" - " + phrase + " -> " + strings[phrase]);
}
}

}

0 comments on commit 9db1df8

Please sign in to comment.