Skip to content
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

[Translator] How to properly use UX Translator from a Bundle? #1034

Open
asdfMaciej opened this issue Aug 4, 2023 · 3 comments
Open

[Translator] How to properly use UX Translator from a Bundle? #1034

asdfMaciej opened this issue Aug 4, 2023 · 3 comments
Labels

Comments

@asdfMaciej
Copy link

asdfMaciej commented Aug 4, 2023

Hello,

I'm creating a bundle with the following assets/ directory structure:
image

I have manually copied translator.js from existing application code, and modified it to use different relative paths:

import { localeFallbacks } from '../../../var/translations/configuration';
import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-translator';

setLocaleFallbacks(localeFallbacks);

export { trans };

export * from '../../../var/translations';

The paths are relative to the node_modules directory, as during yarn build the file resides in /node_modules/@company-name/bundle-name/translator.js. The path isn't relative to the vendor/ directory, autocomplete gets it wrong.

Is this the correct approach to use UX Translator within a bundle? Or is there a better approach? Depending on the directory structure feels brittle to me and I wasn't able to find an answer regarding this topic.

I'm creating an Issue instead asking on Stack Overflow, as I would love to see this in the official documentation.

Thanks in advance :)

@asdfMaciej asdfMaciej changed the title How to properly use UX Translator from a Bundle? [Translator] How to properly use UX Translator from a Bundle? Aug 4, 2023
@Kocal
Copy link
Contributor

Kocal commented Aug 4, 2023

Hi,

The Symfony UX Translator was designed with Symfony apps support in mind, and not bundles. Anyway, the Translator must not be installed in your bundle, it's not its responsability IMO.

A bundle won't be able to import translations directly, from your app.

However your app can initialize your bundle script and inject the needed translations:

  1. Your bundle should not require the UX Translator
  2. Your bundle must define its translations (in translations/ directory)
  3. Your bundle must keep its assets uncompiled, this is needed for later
  4. Your app must require the UX Translator, it will dump your app+bundles translations in /var/translations/... files
  5. Your app must initialize your bundle's asset and inject the needed translations:
// assets/foo.js
import { FIRST_TRANSLATION_FROM_BUNDLE, SECOND_TRANSLATION_FROM_BUNDLE } from './translator';
import { initBundle } from 'path-to-your-bundle'; // your bundle must provide this kind of function

initBundle({
  translations: { FIRST_TRANSLATION_FROM_BUNDLE, SECOND_TRANSLATION_FROM_BUNDLE }
});

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants