我有以下檔案格式:
$language['exemple1']['address_postal'] = "Postal code";
$language['exemple2']['address_city'] = "City";
$language['exemple3']['hello'] = "Hello %s :)";
$language['exemple4']['custom'] = "Hello \"%s\" :)
<br/> hope you are doing<b style=\"font-size:5px\">%s </b>
";
我需要翻譯“正確的部分”,例如:= "my translation";在多種語言中,正如您在示例 4 中看到的那樣,翻譯這樣的檔案看起來很困難。
我想過用 DeepL 翻譯整個檔案,然后使用正則運算式替換$language[][]...將由未翻譯的左部分翻譯的左部分,以便我可以使用它,但我不知道如何“獲取”帶有正則運算式的左側部分
uj5u.com熱心網友回復:
不漂亮,但我想我知道你想要什么。
我猜你有很多行|檔案。所以這可能是一個簡單的解決方案。
您可能需要稍微玩一下地圖。
您需要找到一些在翻譯中不會被觸及的占位符。當我嘗試時,
Fe {“迷失在翻譯中”。
玩得開心=)
// Load PHP file into string.
$string = file_get_contents('tmp/array.php');
// Remove <?php
$parts = explode('<?php', $string);
// Get array code-string without leading and trailing line breaks.
$string = trim($parts[1], PHP_EOL);
// Create a map of characters that need to be replaced before translation.
$map = [
'\\' => '##BACKSLASH##',
'/' => '##SLASH##',
// add more if you have to
// Note: i tried {..} and {{..}} and those get "lost in translation" :)
];
// Replace characters with placeholders.
$string = str_replace(array_keys($map), array_values($map), $string);
echo $string . PHP_EOL;
// $language['exemple1']['address_postal'] = "Postal code";
// $language['exemple2']['address_city'] = "City";
// $language['exemple3']['hello'] = "Hello %s :)";
// $language['exemple4']['custom'] = "Hello ##BACKSLASH##"%s##BACKSLASH##" :)
// <br##SLASH##> hope you are doing<b style=##BACKSLASH##"font-size:5px##BACKSLASH##">%s <##SLASH##b>
// ";
// Translated to:
// $language['exemple1']['address_postal'] = "Postleitzahl";
// $language['exemple2']['address_city'] = "Stadt";
// $language['exemple3']['hello'] = "Hallo %s :)";
// $language['exemple4']['custom'] = "Hallo ##BACKSLASH##"%s##BACKSLASH##" :)
// <br##SLASH##> ich hoffe, es geht Ihnen gut<b style=##BACKSLASH##"font-size:5px##BACKSLASH##">%s <##SLASH##b>
// ";
// Put translated string into another file and replace back.
$string = file_get_contents('tmp/translated.txt');
$string = str_replace(array_values($map), array_keys($map), $string);
echo $string . PHP_EOL;
// $language['exemple1']['address_postal'] = "Postleitzahl";
// $language['exemple2']['address_city'] = "Stadt";
// $language['exemple3']['hello'] = "Hallo %s :)";
// $language['exemple4']['custom'] = "Hallo \"%s\" :)
// <br/> ich hoffe, es geht Ihnen gut<b style=\"font-size:5px\">%s </b>
// ";
uj5u.com熱心網友回復:
你可以這樣做:
$language['hello_world'] = [
'en' => 'Hello world',
'fr' => 'Bonjour le monde',
.......
],
... a以及您將在哪里使用它,您只需撰寫:
$language['hello_world']['en']
^ the phrase ^ the language you need
在我的網站中,我使用不同的語言環境檔案,例如:en.php、fr.php,并且我只有 1 個包含所有短語的陣列。我以同樣的方式接受它們,我只是更改了所選語言要呼叫的檔案的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/361191.html
