-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for auto-loading vendor packages language paths #181
base: main
Are you sure you want to change the base?
Conversation
Hey @Haruki1707 really nice PR, can you create like a dummy repo for me to try that with some vendor languages? Sorry for the delay on this. Thanks. |
Please could you close this PR? I need this 👍 Tested on a Inertia/Laravel 11 project and seems to work fine (with a package i developed) |
Any plans on closing the issue? |
Hi @plokko, Sorry, I haven’t had the chance to finish this pull request yet. The main issue I encountered is with determining the location of language files for packages and libraries. If a package registers its language files in the Service Provider following Laravel's conventions (e.g., placing them in the Additionally, Laravel allows for customizing the key used to reference language files. By default, language strings can be accessed via To complicate things further, some packages (like Spatie’s) extend Laravel's base Service Provider to add custom functionality. This abstraction often changes how they register language files, making it nearly impossible to reliably determine this information solely through JavaScript. A potential solution would be to implement a small piece of PHP code to extract this information directly from Laravel and provide it as a JSON or similar format for this package. However, not sure if this would deviate from the current design goal of handling everything on the JavaScript side in this i18n package. Some help with solving this would be greatly appreciated! |
Yeah i was thinking the same: I have a proposition to fix the issue: A brief example: //...
i18n({
// This will force locale path for vendor packages
resolveVendorLocales: {
'plokko/example-package' : 'path/to/lang' //this points to <root_path>/vendor/plokko/example-package/path/to/lang
},
}),
//... We may even add a package alias like this //...
i18n({
// This will force locale path for vendor packages
resolveVendorLocales: {
'plokko/example-package' : {
alias: 'customName', // will be mapped to customName, ex: customName::lang.string
path: 'path/to/lang' //this points to <root_path>/vendor/plokko/example-package/path/to/lang
},
}),
//... |
Before completely giving up on retrieving the data directly from Laravel, I experimented with creating a standalone PHP script to extract vendor language information. Here's what it looks like: <?php
require __DIR__.'/../../../../vendor/autoload.php';
// Bootstrap the Laravel application
$app = require_once __DIR__.'/../../../../bootstrap/app.php';
// Access the console kernel to initialize Laravel (this mimics the artisan behavior)
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
// Ensure that Laravel is booted by running a `dummy` command (this is like executing artisan commands)
$kernel->bootstrap();
// Laravel application is now booted and can execute any laravel code as artisan commands would
$translationLoader = $app->make(Illuminate\Translation\Translator::class)->getLoader();
if ($translationLoader instanceof Illuminate\Contracts\Translation\Loader) {
exit(json_encode($translationLoader->namespaces()));
} else {
exit(1);
} I tested executing this script using Node.js's execSync function, and it worked as expected: const scriptPath = process.cwd() + '/node_modules/laravel-vue-i18n/src/utils/packages-translation-namespaces.php'
const result = execSync('php ' + scriptPath)
return Object.entries(JSON.parse(result.toString()))
.map(([key, value]) => ({
name: String(key),
langPath: String(value)
})) However, using this approach would require mocking the script's response during testing, as we don't have access to a real Laravel application in the test environment. That said, since this method relies on Laravel's well-established internal mechanisms for registering language files, it's unlikely to break or change unexpectedly. Before proceeding, I’d like your thoughts on executing this PHP script, @xiCO2k. If this approach isn’t ideal, I’ll move forward with @plokko’s proposed solution, even though it could be a bit tedious for users working with packages that don’t follow the expected structure. |
Thanks @Haruki1707 I don't like it too much, because of OS support, so if we can keep it with the js parser will be great. |
This pull request introduces a feature to automatically load all language paths from vendor packages directly from the
vendor
folder without publishing them. Previously if you didn't want to publish the package lang files, the packages paths had to be manually specified in theadditionalLangPaths
option.To maintain compatibility for those already adding the paths manually, this feature is disabled by default. It can be enabled by setting the
loadPackagesLangPaths
option totrue
in the Vite plugin options.An added benefit of using this new feature is that the generated translation key will align with the key used by Laravel in PHP, offering a more consistent and streamlined approach.
Additionally, the README.md has been updated to include the new
loadPackagesLangPaths
option.