Last active
March 13, 2018 10:28
-
-
Save shaunthegeek/94afd1917656fc420299fa102c8d4959 to your computer and use it in GitHub Desktop.
Google Translation API "Discovering Supported Languages" missing "zh-CN", but "Detecting Languages" return "zh-CN", so we need hack it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require __DIR__ . '/vendor/autoload.php'; | |
| use Google\Cloud\Translate\TranslateClient; | |
| $targetLanguage = 'en'; // Print the names of the languages in which language? | |
| $translate = new TranslateClient([ | |
| 'keyFilePath' => __DIR__ . '/config/gcloud_key.json', | |
| ]); | |
| $result = $translate->localizedLanguages([ | |
| 'target' => $targetLanguage, | |
| ]); | |
| $langMaps = []; | |
| foreach ($result as $lang) { | |
| $langMaps[$lang['code']] = $lang['name']; | |
| } | |
| echo json_encode($langMaps, JSON_PRETTY_PRINT) . "\n"; | |
| // Detecting the language of a single string | |
| $text = '你好世界'; | |
| $result = $translate->detectLanguage($text); | |
| print("Language code: $result[languageCode]\n"); | |
| print("Confidence: $result[confidence]\n"); | |
| // hack google bug | |
| $code = $result['languageCode']; | |
| if ($result['languageCode'] == 'zh-CN') { | |
| $code = 'zh'; | |
| } | |
| echo 'Language Code(hacked): ' . $code . "\n"; | |
| echo 'Language: ' . $langMaps[$code] . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment