-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: New command lang:sync
#9023
base: 4.6
Are you sure you want to change the base?
Conversation
There are some questions:
|
I don't get the description "When there are translations in the target folder Language/ru, their keys are saved only if they exist in the Language/en folder:". Is that correct? If return [
// Deleted keys
// 'more' => [
// 'nested' => [
// 'key' => 'Example.more.nested.key',
// ],
// ],
'nullableKey' => null,
'numericKey' => 100000,
'status' => [
'error' => 'ru.status.error',
'done' => 'ru.status.done',
'critical' => 'ru.status.critical',
],
'title' => 'ru.title',
'mistakeKey' => 'ru.mistake_key',
]; Then, return [
'mistakeKey' => 'Example.mistakeKey',
'nullableKey' => 'Example.nullableKey',
'numericKey' => 'Example.numericKey',
'status' => [
'error' => 'Example.status.error',
'done' => 'Example.status.done',
'critical' => 'Example.status.critical',
],
'title' => 'Example.title',
]; That is, deleted keys are gone. |
If return [
'title' => 'Default title',
'status' => [
'error' => 'Error!',
'done' => 'Done!',
'critical' => 'Critical!',
],
'nullableKey' => null,
'numericKey' => 100500,
'more' => [
'nested' => [
'key' => 'More nested key...',
],
],
]; Then, return [
'more' => [
'nested' => [
'key' => 'Example.more.nested.key',
],
],
'nullableKey' => 'Example.nullableKey',
'numericKey' => 'Example.numericKey',
'status' => [
'error' => 'Example.status.error',
'done' => 'Example.status.done',
'critical' => 'Example.status.critical',
],
'title' => 'Example.title',
]; That is, the key order changed. |
This command would be useful. |
No. Look at the order of the files. We from EN combine RU. There are translations in RU
Yes. The order has changed. I'm sorting a new array. I think it can be fixed. But the keys will be chaotic when combined. |
This PR branch is too old. Can you rebase? |
Okay, I got what you say.
return [
'title' => 'Default title',
'status' => [
'error' => 'Error!',
'done' => 'Done!',
'critical' => 'Critical!',
],
'nullableKey' => null,
'numericKey' => 100500,
'more' => [
'nested' => [
'key' => 'More nested key...',
],
],
];
<?php
return [
// Deleted keys
// 'more' => [
// 'nested' => [
// 'key' => 'Example.more.nested.key',
// ],
// ],
'nullableKey' => null,
'numericKey' => 100000,
'status' => [
'error' => 'ru.status.error',
'done' => 'ru.status.done',
'critical' => 'ru.status.critical',
],
'title' => 'ru.title',
'mistakeKey' => 'ru.mistake_key',
]; $ php spark lang:sync --locale en --target ru
$ composer cs-fix
return [
'more' => [
'nested' => [
'key' => 'Example.more.nested.key',
],
],
'nullableKey' => null,
'numericKey' => 100000,
'status' => [
'error' => 'ru.status.error',
'done' => 'ru.status.done',
'critical' => 'ru.status.critical',
],
'title' => 'ru.title',
]; |
I think the key order should not be changed. Can't you rewrite with recursive calls? <?php
function syncTranslationsRecursive($original, $translations) {
// Initialize an array to store the results
$syncedTranslations = [];
// Loop through the original message array
foreach ($original as $key => $message) {
if (is_array($message)) {
// If the message is an array, recursively synchronize it
$syncedTranslations[$key] = syncTranslationsRecursive(
$message,
array_key_exists($key, $translations) ? $translations[$key] : []
);
} else {
// If the message is not an array, check if the translations array contains the same key
if (array_key_exists($key, $translations)) {
$syncedTranslations[$key] = $translations[$key];
} else {
// If the key doesn't exist in the translations array, set an empty string
$syncedTranslations[$key] = '';
}
}
}
return $syncedTranslations;
}
// Original message array
$original = [
'title' => 'Default title',
'status' => [
'error' => 'Error!',
'done' => 'Done!',
'critical' => 'Critical!',
],
'more' => [
'nested' => [
'key' => 'More nested key...',
],
],
];
// Translated message array (partially translated)
$translations = [
'title' => 'Título predeterminado',
'status' => [
'error' => '¡Error!',
'done' => '¡Hecho!',
],
];
// Synchronize the translations array with the original message array
$syncedTranslations = syncTranslationsRecursive($original, $translations);
// Print the results
print_r($syncedTranslations); |
cbd2d0f
to
19f9440
Compare
What is the disadvantage of the current array merge? I can just remove the sorting and everything looks like you wanted. But, if the target file has keys, then they are added to the end. |
I would like the key order is never changed in any case. |
19f9440
to
cf93a62
Compare
Results based on the data above: // translation RU is empty
return [
'title' => 'Example.title',
'status' => [
'error' => 'Example.status.error',
'done' => 'Example.status.done',
'critical' => 'Example.status.critical',
],
'nullableKey' => 'Example.nullableKey',
'numericKey' => 'Example.numericKey',
'more' => [
'nested' => [
'key' => 'Example.more.nested.key',
],
],
]; // translation RU exist
return [
'nullableKey' => null,
'numericKey' => 100000,
'status' => [
'error' => 'ru.status.error',
'done' => 'ru.status.done',
'critical' => 'ru.status.critical',
],
'title' => 'ru.title',
'more' => [
'nested' => [
'key' => 'Example.more.nested.key',
],
],
]; |
When translation RU exist, the key order will be different from EN. |
cf93a62
to
6bc2c49
Compare
// Before: app/Language/en/Example.php
return [
'title' => 'Default title',
'status' => [
'error' => 'Error!',
'done' => 'Done!',
'critical' => 'Critical!',
],
'nullableKey' => null,
'array' => [],
'numericKey' => 100500,
'more' => [
'nested' => [
'key' => 'More nested key...',
],
],
]; // Before: app/Language/ru/Example.php
return [
// Deleted keys
// 'more' => [
// 'nested' => [
// 'key' => 'Example.more.nested.key',
// ],
// ],
'nullableKey' => null,
'status' => [
'critical' => 'ru.status.critical',
'done' => 'ru.status.done',
'error' => 'ru.status.error',
],
'mistakeKey' => 'ru.mistake_key',
'numericKey' => 100000,
'title' => 'ru.title',
]; // After: app/Language/ru/Example.php
// 1. Right sort as in EN
// 2. Delete 'mistakeKey' (in EN language not exist)
// 3. Untranslated keys have a placeholder ''Example.more.nested.key'
// 4. Previously translated keys are saved
return [
'title' => 'ru.title',
'status' => [
'error' => 'ru.status.error',
'done' => 'ru.status.done',
'critical' => 'ru.status.critical',
],
'nullableKey' => null,
'array' => [
],
'numericKey' => 100000,
'more' => [
'nested' => [
'key' => 'Example.more.nested.key',
],
],
]; The array intersection search function |
I prefer the following result. To make it easier for the developer to translate the file. // After: app/Language/ru/Example.php
return [
'title' => '(To be translated) Default title',
'status' => [
'error' => '(To be translated) Error!',
'done' => '(To be translated) Done!',
'critical' => '(To be translated) Critical!',
],
'nullableKey' => null,
'array' => [
],
'numericKey' => 100000,
'more' => [
'nested' => [
'key' => '(To be translated) More nested key...',
],
],
]; |
@datamweb I think it's wrong. If the current locale is RU and the translations are the same (EN), it is unclear on the page whether the translation was made or not. Because the fallback translation is EN. Additional text interferes with the preview of the page (too long for some places). Placeholders clearly show untranslated lines. It is possible to make it configurable |
6bc2c49
to
435d8d6
Compare
Okay, what is the status of this feature? It seems like the order of the keys is updated. Personally, I don't think we need the Are there any remaining issues? |
I'll update the PR in a week. From the corrections: Checking the value is a string. In general, we can abandon PR, since I have a separate repository for |
So you would rather add this function to the existing |
If this command is not needed in CI4, I can add it to the optional package. |
435d8d6
to
6872fa5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything seems to work as required by the previous suggestions, so I think it's fine.
Please just change a bit the user guide part.
After working on your translation for the current language for a long time, in some cases you will need to create files for another language. | ||
The ``spark lang:find`` command can be used. But it cannot fully detect absolutely all translations. Especially if the parameters are dynamically set as ``lang('App.status.' . $key, ['payload' => 'John'], 'en')``. | ||
In this case, the best solution is to copy the finished language files and translate them. This way you can save your unique keys that the command did not find. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After working on your translation for the current language for a long time, in some cases you will need to create files for another language. | |
The ``spark lang:find`` command can be used. But it cannot fully detect absolutely all translations. Especially if the parameters are dynamically set as ``lang('App.status.' . $key, ['payload' => 'John'], 'en')``. | |
In this case, the best solution is to copy the finished language files and translate them. This way you can save your unique keys that the command did not find. | |
You may need to create files for another language when you've finished translating for the current language. You can use the spark ``lang:find`` command to help with this. However, it might not detect all translations, particularly those with dynamically set parameters like ``lang('App.status.' . $key, ['payload' => 'John'], 'en')``. | |
To ensure no translations are missed, it's best to copy the completed language files and translate them manually. This approach preserves any unique keys the command might have overlooked. |
Description
A simple command to copy existing translation files to a new locale.
From the file Language/en/Example.php:
After the command
php spark lang:sync --locale en --target ru
we get Language/ru/Example.php .applied
cs-fix
:And so it is with all other translations
=====
When there are translations in the target folder Language/ru, their keys are saved only if they exist in the Language/en folder:
Language/ru/Example.php:
Language/ru/Example.php after sync:
Checklist: