Skip to content

Commit

Permalink
#68 - supporting flow files in other languages
Browse files Browse the repository at this point in the history
- initial working commit for translation engine
- to translate to and from a different language
- base on language definition .csv spreadsheet
- created simple language file for chinese
- simple logic for this initial skeleton
- probably need to consider restricting accidentally replacing
identifiers, and many other considerations
- after refining, integrate into tagui execution flow
  • Loading branch information
kensoh committed Nov 10, 2017
1 parent 1f8f4ce commit 5375704
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/languages/chinese.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
ENGLISH,CHINESE
click,点击
tap,点击
move,移到
hover,移到
type,输入
enter,输入
select,选择
choose,选择
read,读取
fetch,读取
show,显示
print,显示
save,储存
echo,回声
dump,转储
write,写入
snap,快照
table,表格
wait,等待
live,互动
check,验证
download,下载
upload,上载
load,加载
receive,接收
frame,框架
popup,弹出
timeout,倒数
seconds,秒
second,秒
api,api
dom,dom
js,js
as,为
to,到
more than or equals to,多过或等于
greater than or equals to,多过或等于
higher than or equals to,多过或等于
more than or equal to,多过或等于
greater than or equal 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,或
while,当
from,从
for,让
if,如果
35 changes: 35 additions & 0 deletions src/translate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

// 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");
$language = $argv[3]; if ($language=="") die("ERROR - specify language as third parameter\n");

// set language definition array columns base on direction of translation
$direction = strtolower($direction);
if (($direction != 'to') and ($direction != 'from')) die("ERROR - specify to or from as third parameter\n");
if ($direction == 'from') {$column_from = 1; $column_to = 0;} else {$column_from = 0; $column_to = 1;}

// load desired language definition file into array for use in translation
$language = strtolower($language); $language_count = 0; if (file_exists('languages/' . $language . '.csv')) {
$language_file = fopen('languages/' . $language . '.csv','r') or die("ERROR - cannot open " . $language . '.csv' . "\n");
while (!feof($language_file)) {$language_data[$language_count] = fgetcsv($language_file);
if (count($language_data[$language_count]) == 0) die("ERROR - empty row found in " . $language . '.csv' . "\n");
$language_count++;} fclose($language_file); $language_count-=2;} //-1 for header, -1 for EOF

// load automation flow file and perform translation using language definition
$target_flow = $source_flow . '_translated'; // add translated postfix to target flow name
$source_file = fopen($source_flow,'r') or die("ERROR - cannot open " . $source_flow . "\n");
$target_file = fopen($target_flow,'w') or die("ERROR - cannot open " . $target_flow . "\n");
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";
}

?>

0 comments on commit 5375704

Please sign in to comment.