Skip to content

Commit a41424d

Browse files
various fixes (#117)
* fix add new key to json file * fix exclude_files en/book/excluded.php * fix test DIRECTORY_SEPARATOR on WINDOWS * undo factory & add test * fix dropdown file only source translation
1 parent a44cf78 commit a41424d

6 files changed

+47
-15
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ Example: `composer.json`
8787
"type": "path",
8888
"url": "/home/myuser/projects/laravel-translations"
8989
}
90-
]
90+
],
91+
"minimum-stability": "dev"
9192
}
9293
```
9394

src/Actions/CopySourceKeyToTranslationsAction.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44

55
use Outhebox\TranslationsUI\Models\Phrase;
66
use Outhebox\TranslationsUI\Models\Translation;
7+
use Outhebox\TranslationsUI\Models\TranslationFile;
78

89
class CopySourceKeyToTranslationsAction
910
{
1011
public static function execute(Phrase $sourceKey): void
1112
{
1213
Translation::where('source', false)->get()->each(function ($translation) use ($sourceKey) {
14+
$isRoot = TranslationFile::find($sourceKey->file->id)?->is_root;
15+
$locale = $translation->language()->first()?->code;
1316
$translation->phrases()->create([
1417
'value' => null,
1518
'uuid' => str()->uuid(),
1619
'key' => $sourceKey->key,
17-
'group' => $sourceKey->group,
20+
'group' => ($isRoot ? $locale : $sourceKey->group),
1821
'phrase_id' => $sourceKey->id,
1922
'parameters' => $sourceKey->parameters,
20-
'translation_file_id' => $sourceKey->file->id,
23+
'translation_file_id' => ($isRoot ? TranslationFile::firstWhere('name', $locale)?->id : $sourceKey->file->id),
2124
]);
2225
});
2326
}

src/Http/Controllers/SourcePhraseController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ public function create(): Modal
7777
public function store(Request $request): RedirectResponse
7878
{
7979
$connection = config('translations.database_connection');
80+
81+
$key = ['required', 'regex:/^[\w.]+$/u'];
82+
if (TranslationFile::find($request->input('file'))?->extension === 'json') {
83+
$key = ['required', 'string'];
84+
}
85+
8086
$request->validate([
81-
'key' => ['required', 'regex:/^[\w. ]+$/u'],
87+
'key' => $key,
8288
'file' => ['required', 'integer', 'exists:'.($connection ? $connection.'.' : '').'ltu_translation_files,id'],
8389
'content' => ['required', 'string'],
8490
]);
@@ -101,11 +107,16 @@ public function edit(Phrase $phrase): Response|RedirectResponse
101107
return redirect()->route('ltu.phrases.edit', $phrase->uuid);
102108
}
103109

110+
$files = [];
111+
foreach (collect($phrase->where('translation_id', $phrase->translation->id)->get())->unique('translation_file_id') as $value) {
112+
$files[] = TranslationFile::where('id', $value->translation_file_id)->first();
113+
}
114+
104115
return Inertia::render('source/edit', [
105116
'phrase' => PhraseResource::make($phrase),
106117
'translation' => TranslationResource::make($phrase->translation),
107118
'source' => TranslationResource::make($phrase->translation),
108-
'files' => TranslationFileResource::collection(TranslationFile::get()),
119+
'files' => TranslationFileResource::collection($files),
109120
'similarPhrases' => PhraseResource::collection($phrase->similarPhrases()),
110121
]);
111122
}

src/TranslationsManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ public function getTranslations(string $locale): array
7979
return $collection->prepend($rootFileName);
8080
})
8181
->filter(function ($file) use ($locale) {
82-
foreach (config('translations.exclude_files') as $excludeFile) {
82+
foreach (Str::replace('/', DIRECTORY_SEPARATOR, config('translations.exclude_files')) as $excludeFile) {
8383

8484
/**
8585
* <h1>File exclusion by wildcard</h1>
8686
* <h3>$file is with language like <code>en/book/create.php</code> while $excludedFile contains only wildcards or path like <code>book/create.php</code></h3>
8787
* <h3>So, we need to remove the language part from $file before comparing with $excludeFile</h3>
8888
*/
89-
if (fnmatch($excludeFile, str_replace($locale.DIRECTORY_SEPARATOR, '', $file))) {
89+
if (fnmatch($excludeFile, str_replace($locale.DIRECTORY_SEPARATOR, '', $file)) || Str::contains(str_replace($locale.DIRECTORY_SEPARATOR, '', $file), $excludeFile)) {
9090
return false;
9191
}
9292
}

tests/Http/Controllers/SourcePhraseControllerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
]))->assertRedirect(route('ltu.source_translation'));
6262
});
6363

64-
it('can add new source key', function () {
64+
it('can add new source key to php file', function () {
6565
$file = TranslationFile::factory()->create();
6666

6767
$phrase = Phrase::factory()->make([
@@ -76,3 +76,20 @@
7676
'content' => $phrase->value,
7777
])->assertRedirect(route('ltu.source_translation'));
7878
});
79+
80+
it('can add new source key to json file', function () {
81+
$file = TranslationFile::factory()->json()->create(['name' => 'en']);
82+
83+
$phrase = Phrase::factory()->make([
84+
'key' => 'Hello :name',
85+
'translation_id' => $this->translation->id,
86+
'translation_file_id' => $file->id,
87+
]);
88+
89+
$this->actingAs($this->owner, 'translations')
90+
->post(route('ltu.source_translation.store_source_key'), [
91+
'file' => $file->id,
92+
'key' => $phrase->key,
93+
'content' => $phrase->value,
94+
])->assertRedirect(route('ltu.source_translation'));
95+
});

tests/TranslationsManagerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@
6161
$translations = $translationsManager->getTranslations('en');
6262
expect($translations)->toBe([
6363
'en.json' => ['title' => 'My title'],
64-
'en/auth.php' => ['test' => 'Test'],
65-
'en/book/create.php' => ['nested' => 'Nested test'],
64+
'en'.DIRECTORY_SEPARATOR.'auth.php' => ['test' => 'Test'],
65+
'en'.DIRECTORY_SEPARATOR.'book'.DIRECTORY_SEPARATOR.'create.php' => ['nested' => 'Nested test'],
6666
]);
6767

6868
$translations = $translationsManager->getTranslations('');
6969
expect($translations)->toBe([
7070
'en.json' => ['title' => 'My title'],
71-
'en/auth.php' => ['test' => 'Test'],
72-
'en/book/create.php' => ['nested' => 'Nested test'],
71+
'en'.DIRECTORY_SEPARATOR.'auth.php' => ['test' => 'Test'],
72+
'en'.DIRECTORY_SEPARATOR.'book'.DIRECTORY_SEPARATOR.'create.php' => ['nested' => 'Nested test'],
7373
]);
7474
});
7575

@@ -134,16 +134,16 @@
134134
])->has(Phrase::factory()->state([
135135
'phrase_id' => null,
136136
'translation_file_id' => TranslationFile::factory([
137-
'name' => 'book/create',
137+
'name' => 'book'.DIRECTORY_SEPARATOR.'create',
138138
'extension' => 'php',
139139
]),
140140
]))->create();
141141

142142
$translationsManager = new TranslationsManager($filesystem);
143143
$translationsManager->export();
144144

145-
$fileName = lang_path('en/'.$translation->phrases[0]->file->name.'.'.$translation->phrases[0]->file->extension);
146-
$nestedFileName = lang_path('en/'.$nestedTranslation->phrases[0]->file->name.'.'.$nestedTranslation->phrases[0]->file->extension);
145+
$fileName = lang_path('en'.DIRECTORY_SEPARATOR.$translation->phrases[0]->file->name.'.'.$translation->phrases[0]->file->extension);
146+
$nestedFileName = lang_path('en'.DIRECTORY_SEPARATOR.$nestedTranslation->phrases[0]->file->name.'.'.$nestedTranslation->phrases[0]->file->extension);
147147

148148
$fileNameInDisk = File::allFiles(lang_path($translation->language->code))[0]->getPathname();
149149
$nestedFileNameInDisk = File::allFiles(lang_path($nestedTranslation->language->code))[1]->getPathname();

0 commit comments

Comments
 (0)