Skip to content

Commit 387d6d9

Browse files
committed
fix: bugs
refactor: Improve covering weird keys
1 parent 98c5a6d commit 387d6d9

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/AI/AIProvider.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ public function __construct(
2929
}
3030

3131
protected function verify(array $list): void {
32-
$sourceKeys = collect($this->strings)->keys()->unique()->sort()->values()->toArray();
33-
$resultKeys = collect($list)->pluck('key')->unique()->sort()->values()->toArray();
32+
$sourceKeys = collect($this->strings)->keys()->unique()->sort()->values();
33+
$resultKeys = collect($list)->pluck('key')->unique()->sort()->values();
3434

35-
if ($sourceKeys !== $resultKeys) {
36-
throw new VerifyFailedException("Failed to translate the string. The keys are not matched. (Source: " . implode(', ', $sourceKeys) . ") (Result: " . implode(', ', $resultKeys) . ")");
35+
$diff = $sourceKeys->diff($resultKeys);
36+
37+
if ($diff->count() > 0) {
38+
// \Log::error("Failed to translate the string. The keys are not matched. (Diff: {$diff->implode(', ')})");
39+
throw new VerifyFailedException("Failed to translate the string. The keys are not matched. (Diff: {$diff->implode(', ')})");
3740
}
3841

3942
foreach ($list as $item) {
@@ -71,7 +74,7 @@ protected function getUserPrompt($replaces = []) {
7174
'targetLanguage' => $this->targetLanguage,
7275
'filename' => $this->filename,
7376
'parentKey' => basename($this->filename, '.php'),
74-
'keys' => collect($this->strings)->keys()->implode(", "),
77+
'keys' => collect($this->strings)->keys()->implode("`, `"),
7578
'strings' => collect($this->strings)->map(function ($string, $key) {
7679
if (is_string($string)) {
7780
return " - `{$key}`: \"\"\"{$string}\"\"\"";

src/AI/prompt-system.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Follow these additional rules:
2323
- Keep the length almost the same.
2424
- Keep the words forms same. Don't change the tense or form of the words.
2525
- Don't translate codes(`code`), variables, commands(/command), placeholders, and html tags.
26+
- Keep the html codes like <0>, </0>, <1>, </1>. Don't remove.
2627
- For time expressions:
2728
- Translate time-related phrases like "Updated at :time" by adjusting the variable position to fit the target language's natural word order while preserving the meaning.
2829
- For count expressions:

src/AI/prompt-user.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The most important mission: All strings should be translated into {targetLanguag
66

77
# Content
88
- Parent key (Only for your reference, don't put to the key): `{parentKey}`
9-
- To make sure, the keys you should translate are: {keys}
9+
- To make sure, the keys you should translate are: `{keys}`
10+
- [CRITICAL] Sometimes key has double quotes, '’', '"', '{', '}', ':' or other special characters. You should keep them in the translation.
1011
- These are the strings that you should translate into the target language. The source strings: (e.g. `key`: """(value)""")
1112
{strings}

src/Console/TranslateCrowdin.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ public function translate() {
312312
$this->line(" Retrieving strings...");
313313
$allStrings = $this->getAllSourceString($this->selectedProject['id'], $file['id']);
314314

315+
$this->line(" Retrieving translations...");
316+
$allTranslations = $this->getAllLanguageTranslations($this->selectedProject['id'], $file['id'], $targetLanguage['id']);
317+
315318
$this->line(" Retrieving approvals...");
316319
$approvals = $this->getApprovals($this->selectedProject['id'], $file['id'], $targetLanguage['id']);
317320

@@ -338,17 +341,21 @@ public function translate() {
338341
});
339342

340343
$untranslatedStrings = $allStrings
341-
->filter(function (SourceString $sourceString) use ($approvals) {
344+
->filter(function (SourceString $sourceString) use ($approvals, $allTranslations) {
342345
if (!$sourceString->getIdentifier()) return false;
343346

344347
if ($sourceString->isHidden()) {
345-
// $this->line(" Skip: {$sourceString->getIdentifier()}: {$sourceString->getText()} (hidden)");
348+
// $this->line(" Skip: {$sourceString->getIdentifier()}: {$sourceString->getText()} (hidden)");
346349
return false;
347350
}
348351

349-
if (!$approvals->filter(fn(StringTranslationApproval $ap) => $ap->getStringId() == $sourceString->getId())->isEmpty()) {
350-
// $this->line(" Skip: {$sourceString->getIdentifier()}: {$sourceString->getText()} (approved)");
351-
return false;
352+
$approved = $approvals->filter(fn(StringTranslationApproval $ap) => $ap->getStringId() == $sourceString->getId());
353+
if ($approved->count() > 0) {
354+
$translation = $allTranslations->filter(fn(LanguageTranslation $t) => $t->getTranslationId() == $approved->first()->getTranslationId())->first();
355+
if ($translation) {
356+
$this->line(" Skip: {$sourceString->getIdentifier()}: {$sourceString->getText()} (approved)");
357+
return false;
358+
}
352359
}
353360

354361
return true;
@@ -397,6 +404,10 @@ public function translate() {
397404

398405
foreach ($translated as $item) {
399406
$targetString = $untranslatedStrings->where('identifier', $item->key)->first();
407+
if (!$targetString) {
408+
$this->info("Skipping translation: {$item->key} (Not found)");
409+
continue;
410+
}
400411

401412
$existsTranslations = $this->getAllTranslations($this->selectedProject['id'], $targetString['id'], $targetLanguage['id']);
402413
$existsTranslations = $existsTranslations->sortByDesc(fn(StringTranslation $t) => Carbon::make($t->getDataProperty('created_at')))->values();

0 commit comments

Comments
 (0)