willdurand/js-translation-bundle
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require willdurand/js-translation-bundle
Add to config/bundles.php:
return [
// ...
WillDurand\JsTranslationBundle\WillDurandJsTranslationBundle::class => ['all' => true],
];
Configuration: Publish the default config:
php bin/console config:dump-reference WillDurandJsTranslationBundle
Or customize in config/packages/willdurand_js_translation.yaml:
willdurand_js_translation:
output_path: '%kernel.project_dir%/public/js/translations'
domains: ['messages', 'validators']
json_encode_options: 0
jsonp_callback: null
ignore_patterns: ['%kernel.project_dir%/vendor']
include_original_values: false
First Use Case: Dump translations to JS:
php bin/console assets:install public/
php bin/console willdurand:js-translation:dump
This generates a translations.js file in public/js/translations/ with all translation keys.
Usage in JavaScript:
// Load translations.js in your frontend (e.g., via asset pipeline)
const translations = window.translations; // Access via global variable
console.log(translations.messages.hello); // Output: "Hello!"
Translation Dumping:
wildurand:js-translation:dump to generate translations.js with all loaded translation domains.post-update or post-deploy hook (e.g., via Symfony’s CronBundle or Git hooks).Integration with Frontend:
translations.js in your main JS bundle (e.g., Webpack Encore, Vite, or Laravel Mix):
// webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
.copy('public/js/translations.js', 'public/js/translations.js');
fetch('/js/translations.js')
.then(response => response.text())
.then(text => {
const script = document.createElement('script');
script.textContent = text;
document.head.appendChild(script);
});
Domain-Specific Translations:
domains in willdurand_js_translation.yaml to include only necessary domains (e.g., ['messages', 'validators']).console.log(translations.validators.required); // "This value should not be blank."
Localization in Templates:
trans filter in Twig to ensure consistency:
{{ 'hello'|trans({ '%name%': user.name }, 'messages') }}
Parameterized Translations:
php bin/console willdurand:js-translation:dump --locale=en --locale=fr
console.log(translations.messages.hello.name); // "Hello, %name%!"
JSONP Support:
willdurand_js_translation:
jsonp_callback: callback
const script = document.createElement('script');
script.src = `/js/translations.js?callback=handleTranslations`;
document.body.appendChild(script);
function handleTranslations(data) {
console.log(data.messages.hello);
}
Custom Output Paths:
output_path to integrate with your asset pipeline:
willdurand_js_translation:
output_path: '%kernel.project_dir%/public/build/assets/translations'
Excluding Vendor Translations:
ignore_patterns to exclude third-party translations:
willdurand_js_translation:
ignore_patterns: ['%kernel.project_dir%/vendor/*', '%kernel.project_dir%/node_modules/*']
Translation Namespaces:
.po/.yml files:
messages:
auth:
login: "Login"
console.log(translations.messages.auth.login);
Dynamic Locale Switching:
// Load translations for the current locale
fetch(`/js/translations_${currentLocale}.js`)
.then(response => response.text())
.then(text => {
const script = document.createElement('script');
script.textContent = text;
document.head.appendChild(script);
});
Integration with Frontend Frameworks:
// Vue example
export default {
data() {
return {
translations: window.translations.messages
};
}
};
i18next.init({
resources: {
en: { translation: window.translations.messages }
}
});
Translation Updates Without Full Reload:
translations_v2.js) and cache-busting:
willdurand_js_translation:
output_path: '%kernel.project_dir%/public/js/translations_v{{ date("YmdHis") }}.js'
Translation Keys Mismatch:
.po, .yml, .xliff). Use the same domain and namespace.translations.js with those used in {{ 'key'|trans }} in Twig.Locale-Specific Files:
en only, even though other locales are configured.php bin/console willdurand:js-translation:dump --locale=en --locale=fr
framework.default_locale in config/packages/framework.yaml matches your expected locale.Caching Issues:
php bin/console cache:clear
output_path is writable by the web server.JSONP Security:
jsonp_callback is user-controlled.jsonp_callback to a fixed value or disable it in production:
willdurand_js_translation:
jsonp_callback: null # Disable JSONP in production
Large Translation Files:
translations.js becomes too large, slowing down page loads.auth.js, forms.js).json_encode_options:
willdurand_js_translation:
json_encode_options: JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
Original Values Leak:
include_original_values only for debugging:
willdurand_js_translation:
include_original_values: false # Default
Symfony 6+ Compatibility:
WillDurandJsTranslationBundle is listed in config/bundles.php.config/packages/ directory for configuration (Symfony 5+ style).translations.js file for expected keys:How can I help you explore Laravel packages today?