Skip to content

Commit 5267e76

Browse files
committed
try translate
1 parent 70efc5c commit 5267e76

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

translate/src/translateHtml.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
22+
*/
23+
24+
// Include Google Cloud dependendencies using Composer
25+
require_once __DIR__ . '/../vendor/autoload.php';
26+
// require_once __DIR__ . '/chinese.php';
27+
// if (count($argv) < 2 || count($argv) > 3) {
28+
// return printf("Usage: php %s TEXT [TARGET_LANGUAGE]\n", __FILE__);
29+
// }
30+
// list($_, $text) = $argv;
31+
$targetLanguage = 'en';
32+
33+
// [START translate_translate_text]
34+
use Google\Cloud\Translate\TranslateClient;
35+
36+
/** Uncomment and populate these variables in your code */
37+
// $text = 'The text to translate.';
38+
// $targetLanguage = 'ja'; // Language to translate to
39+
40+
// print(sizeof($_LANG['store']));
41+
42+
$translate = new TranslateClient();
43+
44+
// $ks = array('store');
45+
// function loopArray($ar) {
46+
// global $translate;
47+
// global $ks;
48+
// // print_r($ks);
49+
// foreach ($ar as $key => $value) {
50+
// if(is_array($value)) {
51+
// // print("['$key']");
52+
// array_push($ks, $key);
53+
// // print_r($ks);
54+
// loopArray($value);
55+
// array_pop($ks);
56+
// } else {
57+
// // print_r($ks);
58+
// print('$_LANG');
59+
// foreach ($ks as $ksval) {
60+
// print("['$ksval']");
61+
// }
62+
// print("['$key'] = \"");
63+
// $result = $translate->translate($value, [
64+
// 'target' => 'zh_TW',
65+
// ]);
66+
// print("$result[text]\" \n\n");
67+
// //
68+
// // print('$_LANG[\'store\']');
69+
// }
70+
// }
71+
// }
72+
73+
$homepage = file_get_contents('./index.tpl');
74+
$result = $translate->translate($homepage, [
75+
'target' => 'zh_TW',
76+
]);
77+
78+
foreach ($result as $key => $value) {
79+
print($value);
80+
}
81+
82+
83+
84+
// loopArray($_LANG['store']);
85+
86+
87+
88+
// print("Source language: $result[source]\n");
89+
// print("Translation: $result[text]\n");
90+
// [END translate_translate_text]

translate/src/translatew.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate/README.md
22+
*/
23+
24+
// Include Google Cloud dependendencies using Composer
25+
require_once __DIR__ . '/../vendor/autoload.php';
26+
require_once __DIR__ . '/chinese.php';
27+
// if (count($argv) < 2 || count($argv) > 3) {
28+
// return printf("Usage: php %s TEXT [TARGET_LANGUAGE]\n", __FILE__);
29+
// }
30+
// list($_, $text) = $argv;
31+
$targetLanguage = 'en';
32+
33+
// [START translate_translate_text]
34+
use Google\Cloud\Translate\TranslateClient;
35+
36+
/** Uncomment and populate these variables in your code */
37+
// $text = 'The text to translate.';
38+
// $targetLanguage = 'ja'; // Language to translate to
39+
40+
// print(sizeof($_LANG['store']));
41+
42+
$translate = new TranslateClient();
43+
44+
$ks = array('store');
45+
function loopArray($ar) {
46+
global $translate;
47+
global $ks;
48+
// print_r($ks);
49+
foreach ($ar as $key => $value) {
50+
if(is_array($value)) {
51+
// print("['$key']");
52+
array_push($ks, $key);
53+
// print_r($ks);
54+
loopArray($value);
55+
array_pop($ks);
56+
} else {
57+
// print_r($ks);
58+
print('$_LANG');
59+
foreach ($ks as $ksval) {
60+
print("['$ksval']");
61+
}
62+
print("['$key'] = \"");
63+
$result = $translate->translate($value, [
64+
'target' => 'zh_TW',
65+
]);
66+
print("$result[text]\" \n\n");
67+
//
68+
// print('$_LANG[\'store\']');
69+
}
70+
}
71+
}
72+
73+
loopArray($_LANG['store']);
74+
75+
76+
77+
// print("Source language: $result[source]\n");
78+
// print("Translation: $result[text]\n");
79+
// [END translate_translate_text]

0 commit comments

Comments
 (0)