Skip to content

Commit 513a402

Browse files
authored
Allow import translations with same keys in diffrent groups. (#155)
1 parent ca2a6d5 commit 513a402

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

src/Actions/SyncPhrasesAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function execute(Translation $source, $key, $value, $locale, $file
4444
], [
4545
'value' => (empty($value) ? null : $value),
4646
'parameters' => getPhraseParameters($value),
47-
'phrase_id' => $translation->source ? null : $source->phrases()->where('key', $key)->first()?->id,
47+
'phrase_id' => $translation->source ? null : $source->phrases()->where('key', $key)->where('group', $translationFile->name)->first()?->id,
4848
]);
4949
}
5050
}

src/Console/Commands/ImportTranslationsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function syncMissingTranslations(Translation $source, string $locale): vo
139139
$source->load('phrases.translation', 'phrases.file');
140140

141141
$source->phrases->each(function ($phrase) use ($translation, $locale) {
142-
if (! $translation->phrases()->where('key', $phrase->key)->first()) {
142+
if (! $translation->phrases()->where('key', $phrase->key)->where('group', $phrase->group)->first()) {
143143
$fileName = $phrase->file->name.'.'.$phrase->file->extension;
144144

145145
if ($phrase->file->name === config('translations.source_language')) {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Outhebox\TranslationsUI\Actions\SyncPhrasesAction;
4+
use Outhebox\TranslationsUI\Models\Language;
5+
use Outhebox\TranslationsUI\Models\Phrase;
6+
use Outhebox\TranslationsUI\Models\Translation;
7+
8+
it('can sync phrases with same keys in different groups', function () {
9+
// Setup
10+
$english = Language::factory()
11+
->create([
12+
'rtl' => false,
13+
'code' => 'en',
14+
'name' => 'English',
15+
]);
16+
17+
$dutch = Language::factory()
18+
->create([
19+
'rtl' => false,
20+
'code' => 'nl',
21+
'name' => 'Nederlands',
22+
]);
23+
24+
$englishTranslation = Translation::factory()
25+
->source()
26+
->create([
27+
'language_id' => $english->id,
28+
]);
29+
30+
$englighAuthorsTitle = Phrase::factory()->create([
31+
'key' => 'title',
32+
'translation_id' => $englishTranslation->id,
33+
'group' => 'authors',
34+
'value' => 'Authors', // Dutch: Schrijvers
35+
]);
36+
37+
$englighBooksTitle = Phrase::factory()->create([
38+
'key' => 'title',
39+
'translation_id' => $englishTranslation->id,
40+
'group' => 'books',
41+
'value' => 'Books', // Dutch: Boeken
42+
]);
43+
44+
Phrase::bootHasUuid(); // IDK why this is needed... without this we get an integrity constraint violation for empty uuid.
45+
46+
// When
47+
SyncPhrasesAction::execute(
48+
$englishTranslation,
49+
'title',
50+
'Schrijvers',
51+
'nl',
52+
'authors.php',
53+
);
54+
55+
SyncPhrasesAction::execute(
56+
$englishTranslation,
57+
'title',
58+
'Boeken',
59+
'nl',
60+
'books.php',
61+
);
62+
63+
// Then
64+
$dutchTranslation = $dutch->translation;
65+
expect($dutchTranslation)->toBeInstanceOf(Translation::class);
66+
67+
$dutchPhrases = $dutchTranslation->phrases;
68+
expect($dutchPhrases)->toHaveCount(2);
69+
70+
expect($dutchPhrases[0]->phrase_id)->toEqual($englighAuthorsTitle->id);
71+
expect($dutchPhrases[0]->key)->toEqual($englighAuthorsTitle->key);
72+
expect($dutchPhrases[0]->group)->toEqual($englighAuthorsTitle->group);
73+
expect($dutchPhrases[0]->value)->toEqual('Schrijvers');
74+
75+
expect($dutchPhrases[1]->phrase_id)->toEqual($englighBooksTitle->id);
76+
expect($dutchPhrases[1]->key)->toEqual($englighBooksTitle->key);
77+
expect($dutchPhrases[1]->group)->toEqual($englighBooksTitle->group);
78+
expect($dutchPhrases[1]->value)->toEqual('Boeken');
79+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Outhebox\TranslationsUI\Models\Language;
4+
use Outhebox\TranslationsUI\Models\Phrase;
5+
use Outhebox\TranslationsUI\Models\Translation;
6+
7+
beforeEach(function () {
8+
App::useLangPath(__DIR__.'lang_test');
9+
createDirectoryIfNotExits(lang_path());
10+
});
11+
12+
it('can import translations with same keys in diffent groups', function () {
13+
// Given
14+
$english = Language::factory()
15+
->create([
16+
'rtl' => false,
17+
'code' => 'en',
18+
'name' => 'English',
19+
]);
20+
21+
$dutch = Language::factory()
22+
->create([
23+
'rtl' => false,
24+
'code' => 'nl',
25+
'name' => 'Nederlands',
26+
]);
27+
28+
createPhpLanguageFile('en/authors.php', [
29+
'title' => 'Authors'
30+
]);
31+
32+
createPhpLanguageFile('en/books.php', [
33+
'title' => 'Books',
34+
]);
35+
36+
createPhpLanguageFile('nl/unrelated.php', []);
37+
38+
Phrase::bootHasUuid(); // IDK why this is needed... without this we get an integrity constraint violation for empty uuid.
39+
40+
// When
41+
$this->artisan('translations:import')
42+
->assertExitCode(0);
43+
44+
// Then
45+
$translation = $dutch->translation;
46+
expect($translation)->toBeInstanceOf(Translation::class);
47+
48+
$phrases = $translation->phrases;
49+
expect($phrases)->toHaveCount(2);
50+
});

0 commit comments

Comments
 (0)