Skip to content

Commit

Permalink
#68 - supporting flow files in other languages
Browse files Browse the repository at this point in the history
- 2nd iteration reduces mistakes by translating contextually
- 1st iteration is simply search and replace matches
- this iteration uses TagUi’s internal model of TagUI steps and default
syntax in english to make translations only when the translation fits
the internal model
- for example, translating the first phrase/keyword only if the
translated word is part of TagUI steps
- eg translating conditions only if that flow line starts with
condition-starting keywords
- eg translating separators keywords only if they are valid separators
for that corresponding step
- with this commit translating to and from different languages can
retain accuracy much better
  • Loading branch information
kensoh committed Nov 10, 2017
1 parent 5375704 commit 57f03d3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
20 changes: 11 additions & 9 deletions src/languages/chinese.csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,31 @@ dump,转储
write,写入
snap,快照
table,表格
wait,等待
live,互动
check,验证
download,下载
upload,上载
load,加载
receive,接收
frame,框架
popup,弹出
wait,等待
timeout,倒数
seconds,秒
second,秒
api,api
dom,dom
js,js
as,为
to,到
else if,否则如果
else,否则
if,如果
for,让
while,当
check,验证
more than or equals to,多过或等于
greater than or equals to,多过或等于
higher than or equals to,多过或等于
more than or equal to,多过或等于
greater than or equals to,多过或等于
greater than or equal to,多过或等于
higher than or equals to,多过或等于
higher than or equal to,多过或等于
less than or equals to,少过或等于
less than or equal to,少过或等于
Expand All @@ -62,7 +65,6 @@ contains,包括
contain,包括
and,和
or,或
while,当
from,从
for,让
if,如果
to,到
as,为
61 changes: 56 additions & 5 deletions src/translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

/* MULTI-LANGUAGE TRANSLATION SCRIPT FOR TAGUI FRAMEWORK ~ TEBEL.ORG */

// english is used as reference language, define keywords for contextual translation

// list of keywords that are supposed to be at the start of a flow statement
$start_keywords = '|click|tap|move|hover|type|enter|select|choose|read|fetch|show|print|save|echo|dump|write|snap|table|';
$start_keywords.='wait|live|download|upload|load|receive|frame|popup|timeout|api|dom|js|else if|else|if|for|while|check|';

// list of keywords at start of flow statement for valid to and as separators
$to_separator_keywords = '|read|fetch|save|load|dump|write|snap|table|download|receive|for|';
$as_separator_keywords = '|type|enter|select|choose|upload|';

// list of keywords that are supposed to happen specifically after for loop
$forloop_keywords = '|from|';

// list of keywords that are supposed to happen in conditions (after - else if, if, for, while, check)
$conditions_keywords = '|more than or equals to|more than or equal to|greater than or equals to|greater than or equal to|higher than or equals to|higher than or equal to|less than or equals to|less than or equal to|lesser than or equals to|lesser than or equal to|lower than or equals to|lower than or equal to|more than|greater than|higher than|less than|lesser than|lower than|not equals to|not equal to|equals to|equal to|not contains|not contain|contains|contain|and|or|';

// list of keywords at start of flow statement for valid conditions
$start_conditions_keywords = '|else if|if|for|while|check|';

// list of seconds keywords that are supposed to happen after wait and timeout steps
$seconds_keywords = '|seconds|second|'; $start_seconds_keywords = '|wait|timeout|';

// experimental entry point
$source_flow = $argv[1]; if ($source_flow=="") die("ERROR - specify flow filename as first parameter\n");
$direction = $argv[2]; if ($direction=="") die("ERROR - specify to or from as third parameter\n");
Expand All @@ -26,10 +48,39 @@
while(!feof($source_file)) {fwrite($target_file,translate_intent(fgets($source_file)));}
fclose($source_file); fclose($target_file);

function translate_intent($script_line) {
$script_line = ' '.trim($script_line).' '; // use padding to prevent false replacement when a match happens mid-string
for ($language_check = 1; $language_check <= $GLOBALS['language_count']; $language_check++)
$script_line = str_replace(' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].' ',' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].' ',$script_line); return trim($script_line)."\n";
}
// function to perform translation of automation flow by processing each flow line
function translate_intent($script_line) {if ($script_line == "") return ""; // avoid next line character if none
// use special padding to reduce mistakes by preventing false replacement when a match happens mid-string
$script_line = '[START_OF_LINE]'.trim($script_line).'[END_OF_LINE]'; // special padding to be removed later
$start_word = '[NOT_ASSIGNED]'; // used for tracking which start keyword the flow statement starts with

for ($language_check = 1; $language_check <= $GLOBALS['language_count']; $language_check++) {

if (strpos($GLOBALS['start_keywords'],'|'.$GLOBALS['language_data'][$language_check][0].'|')!==false)
{if ($start_word != '[NOT_ASSIGNED]') continue; // skip processing for start keyword if one is already found
$script_line = str_replace('[START_OF_LINE]'.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].' ','[START_OF_LINE]'.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].' ',$script_line,$replace_count);
if ($replace_count > 0) $start_word = $GLOBALS['language_data'][$language_check][0];}

else if (strpos($GLOBALS['conditions_keywords'],'|'.$GLOBALS['language_data'][$language_check][0].'|')!==false) {
if (($start_word != '[NOT_ASSIGNED]') and (strpos($GLOBALS['start_conditions_keywords'],'|'.$start_word.'|')!==false))
$script_line = str_replace(' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].' ',' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].' ',$script_line);}

else if (strpos($GLOBALS['seconds_keywords'],'|'.$GLOBALS['language_data'][$language_check][0].'|')!==false) {
if (($start_word != '[NOT_ASSIGNED]') and (strpos($GLOBALS['start_seconds_keywords'],'|'.$start_word.'|')!==false))
$script_line = str_replace(' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].'[END_OF_LINE]',' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].'[END_OF_LINE]',$script_line);}

else if (strpos($GLOBALS['forloop_keywords'],'|'.$GLOBALS['language_data'][$language_check][0].'|')!==false) {
if ($start_word == 'for') $script_line = str_replace(' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].' ',' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].' ',$script_line);}

else if ($GLOBALS['language_data'][$language_check][0]=='to') {
if (($start_word != '[NOT_ASSIGNED]') and (strpos($GLOBALS['to_separator_keywords'],'|'.$start_word.'|')!==false))
$script_line = str_replace(' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].' ',' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].' ',$script_line);}

else if ($GLOBALS['language_data'][$language_check][0]=='as') {
if (($start_word != '[NOT_ASSIGNED]') and (strpos($GLOBALS['as_separator_keywords'],'|'.$start_word.'|')!==false))
$script_line = str_replace(' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_from']].' ',' '.$GLOBALS['language_data'][$language_check][$GLOBALS['column_to']].' ',$script_line);}}

$script_line = str_replace('[START_OF_LINE]','',str_replace('[END_OF_LINE]','',$script_line));
return trim($script_line)."\n";}

?>

0 comments on commit 57f03d3

Please sign in to comment.