Skip to content

Instantly share code, notes, and snippets.

@futuretap
Created December 17, 2012 20:37
Show Gist options
  • Select an option

  • Save futuretap/4321967 to your computer and use it in GitHub Desktop.

Select an option

Save futuretap/4321967 to your computer and use it in GitHub Desktop.
Script to create a Javascript hash out of all Localizable.strings files. Essential if you're using UIAutomation and want to address buttons etc. by localized title. More info: http://www.innoq.com/blog/phaus/2011/01/using_uiautomation_for_multila.html This is a slightly modified version of the script presented in the blog post that correctly dea…
<?php
//please replace with your settings...
$i18n_folder = "Custom/Resources";
$uiAutomationFolder = "Test/UIAutomationTests/lib";
if(substr($i18n_folder, -1) != "/"){
$i18n_folder.="/";
}
$langs = array();
$ihandle = opendir($i18n_folder);
if ($ihandle) {
while (false !== ($file = readdir($ihandle))) {
if(substr($file, -6) == ".lproj"
&& file_exists ( $i18n_folder."/".$file."/Localizable.strings" )){
$langs[] = $file;
}
}
closedir($ihandle);
}
if(count($langs) > 0){
$l = 0;
$ohandle = fopen($uiAutomationFolder."/Localizables.js", "w");
if($ohandle){
fwrite($ohandle, "if(typeof(UIA) == \"undefined\"){var UIA = {};}\n");
fwrite($ohandle, "if(typeof(application) == \"undefined\"){var application = UIATarget.localTarget().frontMostApp();}\n");
fwrite($ohandle, "UIA.Localizables = {\n");
foreach($langs as $lang){
$l++;
$i = 0;
fwrite($ohandle, "\t\"".substr($lang, 0, -6)."\":{\n");
$localizable = file ($i18n_folder."/".$lang."/Localizable.strings");
foreach($localizable as $line){
$i++;
$rline = mb_convert_encoding($line, "UTF-8", "UTF-16");
if(substr($rline, 0, 1) == "\""){
$larr = explode("=", trim($rline));
fwrite($ohandle, "\t\t".$larr[0].":".substr($larr[1],0,-1));
if($i < count($localizable)){
fwrite($ohandle,",\n");
}else{
fwrite($ohandle, "\n");
}
}
}
fwrite($ohandle, "\t}");
if($l < count($langs)){
fwrite($ohandle, ",\n");
}else{
fwrite($ohandle, "\n");
}
}
fwrite($ohandle, "}\n");
fwrite($ohandle, "\tUIA.getCurrentLang = function(){\n");
fwrite($ohandle, "\t\tif(application.preferencesValueForKey(\"AppleLanguages\")[0] == \"en\")\n");
fwrite($ohandle, "\t\t\treturn \"English\";\n");
fwrite($ohandle, "\t\telse\n");
fwrite($ohandle, "\t\t\treturn application.preferencesValueForKey(\"AppleLanguages\")[0];\n");
fwrite($ohandle, "\t}\n");
fwrite($ohandle, "\tUIA.getCurrentLocalizables = function(){\n");
fwrite($ohandle, "\t\treturn UIA.Localizables[UIA.getCurrentLang()];\n");
fwrite($ohandle, "\t}\n");
fwrite($ohandle, "\n");
fwrite($ohandle, "var Localizable = UIA.getCurrentLocalizables();\n");
fwrite($ohandle, "\n");
fflush($ohandle);
fclose($ohandle);
}
}
?>
@vikulsharma

Copy link
Copy Markdown

How can we use php source in UIAutomation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment