Skip to content

Commit 2cb5076

Browse files
committed
handle 4.0 renamed keys
1 parent 91ba264 commit 2cb5076

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

dev/Commands/AbstractDevCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,17 @@ protected function getNovaKeys(): array
117117

118118
return $sourceKeys;
119119
}
120+
121+
protected function getRenamedNovaKeys(): array
122+
{
123+
// Keys that have been renamed by Nova (from version 4.0)
124+
return [
125+
// Old key => New key
126+
'Forgot Your Password?' => 'Forgot Password',
127+
'Login' => 'Log In',
128+
'Remember Me' => 'Remember me',
129+
'The action ran successfully!' => 'The action was executed successfully.',
130+
'An error occured while uploading the file.' => 'An error occurred while uploading the file.'
131+
];
132+
}
120133
}

dev/Commands/NovaLangCleanup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function handleLocale(string $locale): void
4242

4343
$inputKeys = $this->loadJson($inputFile);
4444

45-
$outputKeys = array_filter($inputKeys, fn ($text) => ! empty(trim($text)) && $text !== static::MISSING_TEXT);
45+
$outputKeys = array_filter($inputKeys, fn ($text) => ! empty(trim($text)) && $text !== static::MISSING_TEXT && ! empty(preg_replace('/\W+/', '', $text)));
4646

4747
$missingKeys = count($inputKeys) - count($outputKeys);
4848

dev/Commands/NovaLangReorder.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
class NovaLangReorder extends AbstractDevCommand
88
{
9-
protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order. The updated file has been output to [%s].';
9+
protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order.';
10+
protected const RENAMED_KEYS = 'Additionally, %d keys were renamed for Nova 4.0.';
11+
protected const OUTPUT_FILE = 'The updated file has been output to [%s].';
1012
protected const NO_KEYS_OUT_OF_ORDER = '"%s" locale has no translation keys out of order.';
1113
protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing %2$s` to add them.';
1214

@@ -43,15 +45,29 @@ public function handleLocale(string $locale)
4345
$inputFile = $this->directoryFrom("$locale.json");
4446
$outputFile = $inputFile;
4547

46-
$localeTranslations = $this->loadJson($inputFile);
48+
$novaRenamedKeys = $this->getRenamedNovaKeys();
49+
50+
$unsortedLocaleTranslations = $this->loadJson($inputFile);
51+
$localeTranslations = [];
52+
53+
$renamedKeys = 0;
54+
55+
foreach ($unsortedLocaleTranslations as $key => $translation) {
56+
if ($newKey = $novaRenamedKeys[$key] ?? false) {
57+
$localeTranslations[$newKey] = $translation;
58+
$renamedKeys++;
59+
} else {
60+
$localeTranslations[$key] = $translation;
61+
}
62+
}
4763

4864
$localeKeys = array_values(array_diff(array_keys($localeTranslations), static::IGNORED_KEYS));
4965

5066
$commonKeys = array_values(array_intersect($this->sourceKeys, $localeKeys));
5167

5268
$diffs = $this->array_diff_order($commonKeys, $localeKeys);
5369

54-
if ($diffs > 0) {
70+
if ($diffs > 0 || $renamedKeys > 0) {
5571
$missingKeys = 0;
5672

5773
$outputKeys = [];
@@ -66,7 +82,14 @@ public function handleLocale(string $locale)
6682

6783
$this->saveJson($outputFile, $outputKeys);
6884

69-
$this->info(sprintf(static::KEYS_OUT_OF_ORDER, $diffs, $locale, $outputFile));
85+
$this->info(sprintf(static::KEYS_OUT_OF_ORDER, $diffs, $locale));
86+
87+
if ($renamedKeys > 0) {
88+
$this->info(sprintf(static::RENAMED_KEYS, $renamedKeys));
89+
}
90+
91+
$this->info(sprintf(static::OUTPUT_FILE, $outputFile));
92+
7093
} else {
7194
$this->info(sprintf(static::NO_KEYS_OUT_OF_ORDER, $locale));
7295

0 commit comments

Comments
 (0)