mirror of
https://github.com/projectsend/projectsend.git
synced 2026-09-16 16:45:07 +00:00
928173e8be
The frontend's catalogue was read straight out of lang/{locale}.json, so
it held exactly the strings this repository owns. That was true for as
long as this repository owned every screen — but the companion packages
own several: Branding, Custom Assets, the whole v1 import. Their strings
have been rendering in English in all sixteen languages, in silence,
because a package catalogue registered through loadJsonTranslationsFrom()
never got as far as the browser.
Asked of the framework's own loader now, which is where that registration
already lands. Same answer as before for this installation — no package
registers a path today, and the merged result is byte-identical to the
file — and the right answer the moment one does.
Precedence comes free and is the useful way round: the loader merges the
application's own catalogue last, so an installation can override a
package's wording without editing the package. There is a test for that,
because it is the kind of ordering that gets reversed by accident.
One thing the test needed and is worth knowing: SetLocale honours an
account's chosen language only while that language is enabled for the
installation, and the Settings cache outlives RefreshDatabase. A test that
sets users.locale and assumes it takes effect gets English and a very
confusing failure.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
77 lines
2.8 KiB
PHP
77 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Platform\Settings\Setting;
|
|
use App\Modules\Platform\Settings\Settings;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
/**
|
|
* A companion package owns whole screens — Branding, Custom Assets, the
|
|
* v1 import — and every string on them was rendering in English in all
|
|
* sixteen languages, because the frontend read lang/{locale}.json
|
|
* directly and a package's catalogue is somewhere else entirely.
|
|
*/
|
|
beforeEach(function () {
|
|
// SetLocale honours the account's own language only while that
|
|
// language is switched on for the installation, and the Settings cache
|
|
// outlives RefreshDatabase — so say it rather than assume it.
|
|
app(Settings::class)->set(Setting::EnabledLocales, ['en', 'es']);
|
|
|
|
$this->admin = User::factory()->create(['locale' => 'es']);
|
|
|
|
$this->packageLang = sys_get_temp_dir().'/package-lang-'.bin2hex(random_bytes(6));
|
|
File::makeDirectory($this->packageLang);
|
|
});
|
|
|
|
afterEach(function () {
|
|
File::deleteDirectory($this->packageLang);
|
|
});
|
|
|
|
function packageCatalogue(string $dir, array $entries): void
|
|
{
|
|
File::put($dir.'/es.json', json_encode($entries, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
|
|
|
|
app('translator')->getLoader()->addJsonPath($dir);
|
|
}
|
|
|
|
function sharedTranslations(User $user): array
|
|
{
|
|
return test()->actingAs($user)->get('/dashboard')->viewData('page')['props']['translations'];
|
|
}
|
|
|
|
test('a package catalogue reaches the frontend', function () {
|
|
packageCatalogue($this->packageLang, ['Attribution settings saved.' => 'Ajustes de atribución guardados.']);
|
|
|
|
expect(sharedTranslations($this->admin))
|
|
->toHaveKey('Attribution settings saved.', 'Ajustes de atribución guardados.');
|
|
});
|
|
|
|
test('the application keeps its own strings', function () {
|
|
packageCatalogue($this->packageLang, ['Something from a package' => 'Algo de un paquete']);
|
|
|
|
$translations = sharedTranslations($this->admin);
|
|
|
|
expect($translations)->toHaveKey('Something from a package')
|
|
// Any string this repository owns, still translated as before.
|
|
->and($translations['Dashboard'] ?? null)->toBe('Panel de control');
|
|
});
|
|
|
|
// The useful way round: an installation can override a package's wording
|
|
// in its own catalogue without editing the package.
|
|
test('the installation wins when both define the same key', function () {
|
|
packageCatalogue($this->packageLang, ['Dashboard' => 'From the package']);
|
|
|
|
expect(sharedTranslations($this->admin)['Dashboard'])->toBe('Panel de control');
|
|
});
|
|
|
|
// English is the key, so there is nothing to send and nothing to merge.
|
|
test('english still ships no catalogue at all', function () {
|
|
packageCatalogue($this->packageLang, ['Attribution' => 'Should never be sent']);
|
|
|
|
$english = User::factory()->create(['locale' => 'en']);
|
|
|
|
expect(sharedTranslations($english))->toBe([]);
|
|
});
|