When developing multi-language supported web applications, we often have configuration files for multiple languages (such as en.php , fr.php , de.php , etc.), and each configuration file contains the translation content of the corresponding language. In actual development, some key values may be encountered where they are not translated, or some keys are missing in the configuration file of a certain language. So, how do you use PHP programs to automatically compare configuration files in multiple languages and find out these untranslated key values?
This article will show how to write a PHP script that compares multiple language configuration files to identify which key values have not been translated, helping developers better manage multilingually.
Suppose we have the following multilingual configuration file:
return [
'welcome_message' => 'Welcome to our website!',
'login' => 'Login',
'register' => 'Register',
];
return [
'welcome_message' => 'Bienvenue sur notre site!',
'login' => 'Se connecter',
];
return [
'welcome_message' => 'Willkommen auf unserer Website!',
'register' => 'Registrieren',
];
As shown above, configuration files for different languages contain some of the same keys (such as welcome_message ), but some keys only appear in some languages (such as login only appears in English and French, but German lacks that key).
To compare these configuration files, we can write a PHP script, load the configuration files of each language and compare them to find out which keys are missing or not translated in the files of some languages.
First, we need to write a function to load the configuration file. These files can be loaded using include or require , assuming that these configuration files all return an associative array.
function loadLangFile($langCode) {
$filePath = __DIR__ . "/$langCode.php";
if (file_exists($filePath)) {
return include $filePath;
}
return [];
}
We need to find all the keys from all the configuration files. For convenience, let's say we already know all supported language codes (such as en , fr , de, etc.). We can iterate through each language profile and combine their bonds together.
$langCodes = ['en', 'fr', 'de'];
$allKeys = [];
foreach ($langCodes as $langCode) {
$langData = loadLangFile($langCode);
$allKeys = array_merge($allKeys, array_keys($langData));
}
$allKeys = array_unique($allKeys); // Go to the heavy,Avoid duplicate keys
Next, we will iterate through all the keys and check whether they have corresponding values in each language configuration file. If a key is missing or empty in the configuration file of a language, we can assume that the key is not translated.
$missingKeys = [];
foreach ($allKeys as $key) {
foreach ($langCodes as $langCode) {
$langData = loadLangFile($langCode);
if (!isset($langData[$key]) || empty($langData[$key])) {
$missingKeys[$langCode][] = $key;
}
}
}
Finally, we can output untranslated keys to help developers quickly locate which keys have not been translated.
echo "Untranslated keys:\n";
foreach ($missingKeys as $langCode => $keys) {
echo "\nIn language $langCode The following key is missing:\n";
foreach ($keys as $key) {
echo "- $key\n";
}
}
Merge all the code together, and the final PHP script looks like this:
function loadLangFile($langCode) {
$filePath = __DIR__ . "/$langCode.php";
if (file_exists($filePath)) {
return include $filePath;
}
return [];
}
$langCodes = ['en', 'fr', 'de'];
$allKeys = [];
foreach ($langCodes as $langCode) {
$langData = loadLangFile($langCode);
$allKeys = array_merge($allKeys, array_keys($langData));
}
$allKeys = array_unique($allKeys); // Go to the heavy,Avoid duplicate keys
$missingKeys = [];
foreach ($allKeys as $key) {
foreach ($langCodes as $langCode) {
$langData = loadLangFile($langCode);
if (!isset($langData[$key]) || empty($langData[$key])) {
$missingKeys[$langCode][] = $key;
}
}
}
echo "Untranslated keys:\n";
foreach ($missingKeys as $langCode => $keys) {
echo "\nIn language $langCode The following key is missing:\n";
foreach ($keys as $key) {
echo "- $key\n";
}
}
Through the above PHP script, we can quickly compare configuration files in multiple languages and find out which key values are not translated. This is very helpful for the maintenance and update of multilingual applications, and can reduce the missed translations and improve the user experience.