Skip to content

Commit

Permalink
various fixes (#117)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adereksisusanto committed May 26, 2024
1 parent a44cf78 commit a41424d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Example: `composer.json`
"type": "path",
"url": "/home/myuser/projects/laravel-translations"
}
]
],
"minimum-stability": "dev"
}
```

Expand Down
7 changes: 5 additions & 2 deletions src/Actions/CopySourceKeyToTranslationsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

use Outhebox\TranslationsUI\Models\Phrase;
use Outhebox\TranslationsUI\Models\Translation;
use Outhebox\TranslationsUI\Models\TranslationFile;

class CopySourceKeyToTranslationsAction
{
public static function execute(Phrase $sourceKey): void
{
Translation::where('source', false)->get()->each(function ($translation) use ($sourceKey) {
$isRoot = TranslationFile::find($sourceKey->file->id)?->is_root;
$locale = $translation->language()->first()?->code;
$translation->phrases()->create([
'value' => null,
'uuid' => str()->uuid(),
'key' => $sourceKey->key,
'group' => $sourceKey->group,
'group' => ($isRoot ? $locale : $sourceKey->group),
'phrase_id' => $sourceKey->id,
'parameters' => $sourceKey->parameters,
'translation_file_id' => $sourceKey->file->id,
'translation_file_id' => ($isRoot ? TranslationFile::firstWhere('name', $locale)?->id : $sourceKey->file->id),
]);
});
}
Expand Down
15 changes: 13 additions & 2 deletions src/Http/Controllers/SourcePhraseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ public function create(): Modal
public function store(Request $request): RedirectResponse
{
$connection = config('translations.database_connection');

$key = ['required', 'regex:/^[\w.]+$/u'];
if (TranslationFile::find($request->input('file'))?->extension === 'json') {
$key = ['required', 'string'];
}

$request->validate([
'key' => ['required', 'regex:/^[\w. ]+$/u'],
'key' => $key,
'file' => ['required', 'integer', 'exists:'.($connection ? $connection.'.' : '').'ltu_translation_files,id'],
'content' => ['required', 'string'],
]);
Expand All @@ -101,11 +107,16 @@ public function edit(Phrase $phrase): Response|RedirectResponse
return redirect()->route('ltu.phrases.edit', $phrase->uuid);
}

$files = [];
foreach (collect($phrase->where('translation_id', $phrase->translation->id)->get())->unique('translation_file_id') as $value) {
$files[] = TranslationFile::where('id', $value->translation_file_id)->first();
}

return Inertia::render('source/edit', [
'phrase' => PhraseResource::make($phrase),
'translation' => TranslationResource::make($phrase->translation),
'source' => TranslationResource::make($phrase->translation),
'files' => TranslationFileResource::collection(TranslationFile::get()),
'files' => TranslationFileResource::collection($files),
'similarPhrases' => PhraseResource::collection($phrase->similarPhrases()),
]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/TranslationsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ public function getTranslations(string $locale): array
return $collection->prepend($rootFileName);
})
->filter(function ($file) use ($locale) {
foreach (config('translations.exclude_files') as $excludeFile) {
foreach (Str::replace('/', DIRECTORY_SEPARATOR, config('translations.exclude_files')) as $excludeFile) {

/**
* <h1>File exclusion by wildcard</h1>
* <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>
* <h3>So, we need to remove the language part from $file before comparing with $excludeFile</h3>
*/
if (fnmatch($excludeFile, str_replace($locale.DIRECTORY_SEPARATOR, '', $file))) {
if (fnmatch($excludeFile, str_replace($locale.DIRECTORY_SEPARATOR, '', $file)) || Str::contains(str_replace($locale.DIRECTORY_SEPARATOR, '', $file), $excludeFile)) {
return false;
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/Http/Controllers/SourcePhraseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
]))->assertRedirect(route('ltu.source_translation'));
});

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

$phrase = Phrase::factory()->make([
Expand All @@ -76,3 +76,20 @@
'content' => $phrase->value,
])->assertRedirect(route('ltu.source_translation'));
});

it('can add new source key to json file', function () {
$file = TranslationFile::factory()->json()->create(['name' => 'en']);

$phrase = Phrase::factory()->make([
'key' => 'Hello :name',
'translation_id' => $this->translation->id,
'translation_file_id' => $file->id,
]);

$this->actingAs($this->owner, 'translations')
->post(route('ltu.source_translation.store_source_key'), [
'file' => $file->id,
'key' => $phrase->key,
'content' => $phrase->value,
])->assertRedirect(route('ltu.source_translation'));
});
14 changes: 7 additions & 7 deletions tests/TranslationsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@
$translations = $translationsManager->getTranslations('en');
expect($translations)->toBe([
'en.json' => ['title' => 'My title'],
'en/auth.php' => ['test' => 'Test'],
'en/book/create.php' => ['nested' => 'Nested test'],
'en'.DIRECTORY_SEPARATOR.'auth.php' => ['test' => 'Test'],
'en'.DIRECTORY_SEPARATOR.'book'.DIRECTORY_SEPARATOR.'create.php' => ['nested' => 'Nested test'],
]);

$translations = $translationsManager->getTranslations('');
expect($translations)->toBe([
'en.json' => ['title' => 'My title'],
'en/auth.php' => ['test' => 'Test'],
'en/book/create.php' => ['nested' => 'Nested test'],
'en'.DIRECTORY_SEPARATOR.'auth.php' => ['test' => 'Test'],
'en'.DIRECTORY_SEPARATOR.'book'.DIRECTORY_SEPARATOR.'create.php' => ['nested' => 'Nested test'],
]);
});

Expand Down Expand Up @@ -134,16 +134,16 @@
])->has(Phrase::factory()->state([
'phrase_id' => null,
'translation_file_id' => TranslationFile::factory([
'name' => 'book/create',
'name' => 'book'.DIRECTORY_SEPARATOR.'create',
'extension' => 'php',
]),
]))->create();

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

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

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

0 comments on commit a41424d

Please sign in to comment.