symfony2: how to manage your translations - symfony

In my symfony2 application, i have set up what's necessary to change the locale.
Now I can use the trans features of the bundle and generate the language files under app/resources/translations
That works fine but I feel it's not efficient to edit yml files by hand, and it's advised to use xliff which is absolutely not user friendly.
Plus, in a collaborative environment, I don't see how this could properly work. I'm not sure translators would like git commands.
To those who have already implemented a translation process : how did you do ? how do you organize it ?
Thanks a lot

I used JMSTranslationBundle https://github.com/schmittjoh/JMSTranslationBundle to manage my translations on a project. You use the usual |trans on the variables you want to translate. And then you use a command with app/console for this bundle, and it builds your yml files for you (you have the option to pick the output file type). It also has a translation UI. And if you add new words, no problem, you just rerun the command, it adds the new words to your existing words without losing the original translation.

I'm using only YAML files, but they are located in each bundle, not in the app folder. This is the better way if you want to get clean code. If your translation files are in the app folder, you need to extract the content for a bundle you want to use in another project.
If you use a good IDE like PhpStorm, YAML is no problem and the easiest way to edit YAML. Don't forget the symfony plugin for PhpStorm!
EDIT:
You can also use this bundle. It provides a web interface for translations: http://jmsyst.com/bundles/JMSTranslationBundle#documentation

I have implemented manageable translations for one of my project you can refer to My answer similar to this topic what i have done i have created a service which loads all the translations from my repository.I have 2 entities for this
Language
Language entity holds all the languages which has 2 properties locale and name
locale (en,fr,ar ...)
name (english,french ...)
LanguageTranslation
LanguageTranslation entity holds all the translations with properties
catalogue
translation
language (translation language )
languageToken (key for translation)
Now all your translations are cached by symfony in prod/dev folder so each time when user updates or adds new translation i have cleared translations cache programmatically i was using sonata admin so i added a call to this function in prePersist and preUpdate you can also use doctrine event listeners prePersist/preUpdate for this
private function clearLanguageCache(){
$cacheDir = __DIR__ . "cache folder path"; /** path to app/cache*/
$finder = new \Symfony\Component\Finder\Finder();
$finder->in(array($cacheDir . "/dev/translations", $cacheDir . "/prod/translations"))->files();
foreach($finder as $file){
unlink($file->getRealpath());
}
}
This part is missing in my previous answer for clearing cache

Related

System Templates version 1.31.0 and higher implementation

I have upgraded my cloud Artifactory to "7.52.0".
Prior to the upgrade I was using System Templates to deploy my pipelines.
Although after the upgrade there is still backward compatibility, The new way to deploy and use System Templates for creating new pipelines is not working for me.
From the release notes I got to this link to configure System Templates in the new way.
https://www.jfrog.com/confluence/display/JFROG/System+Templates
So in my repository A I have 2 files 'pipelines.yml' and 'values.yml'
pipelines.yml is configured as follows:
valuesFilePath: ./values.yml
Include:
template: myTemplates/TestTemplate/1.0.0
My values file contains values for the TestTemplate.
Then I go to https://example.jfrog.io/ui/admin/pipelines/pipelineSources and I try to create a new pipeline from repository A.
Looking at https://example.jfrog.io/ui/pipelines/myPipelines/myPipelines I don't see any pipeline created from the template.
Is that the right way to implement the new System Template?
I have also made sure that the templates are in the Artifactory by checking:
https://example.jfrog.io/ui/pipelines/templates
and also in the Artifactory directory tree.
Currently I am using the REST API in order to CRUD my Template Sources(https://example.jfrog.io/ui/pipelines/sources) and also use the REST API to create a new pipelines sources from a system template (apparently this is the old way).
As after the upgrade creating a source pipeline doesn't sync the old/new templates nor does it create a new pipeline from a system template that is located in the Artifactory.
You need to use the syntax documented in the Global template link.
Using the "jfrog/PublishTemplate" global template documentation
https://www.jfrog.com/confluence/display/JFROG/Global+Templates . I have noticed that in order to create and upload a system template you need to use the following syntax:
valuesFilePath: ./values.yml
include:
template: jfrog/<global_template_name>/<template_version>
According to the system template documentation this is the syntax that got me confused:
valuesFilePath: ./values.yml
Include:
template: jfrog/PublishTemplate/1.0.0
So I have used capital "I" instead of small "i" and bad indentation in order to create a new pipeline from my system template, which failed.
You use the Global template "PublishTemplate" for uploading your system template into your artifactory.
And then use the uploaded templates in order to create your new pipelines.

How to extract translations from a custom vendors bundle

I actually trying to create my own reusable bundle. I created a Demo project that include my custom bundle as dependency using symlink composer config.
Actually, I tried to extract some translation keys from my custom bundle into the demo project.
But when I launch the command bin/console translation:update fr --dump-messages there is no entry from my bundle.
What I want to extract is some constant keys that I want to translate.
For example, in MyCustomClass there is some status in an array. So I don't know if there is an Interface that I can implement to auto extract my translation keys.
I'm using:
Symfony Framework 5.3.8
Symfony Translation 5.3.9
Thanks

Symfony2: Creating entity table conditionally

I have a bundle with entity defined in it. I want to be able to configure this bundle in such a way, that this entity will or won't be relevant. So if bundle is configured properly entity table shouldn't be created with app/console doctrine:schema:update etc, or should be - it should depend on configuration.
How to conditionally "disable" entity so its table won't be created by app/console doctrine:schema:update?
Your scenario requires you to disable the auto_mapping, but it seems to be set to false by default. http://symfony.com/doc/current/reference/configuration/doctrine.html
Next thing to do is make sure the build function of your bundle conditionally adds the wanted DoctrineOrmMappingPass as also is explained here: https://stackoverflow.com/a/26975083/1794894
As you can see in the source, build only is executed once the cache is empty so this is the place where you can do this. You can also take a look at how to add compiler passes there.
I think that although maybe you could find a way, you are complicating your self. If the back-end bundle is independent then always could be optional to install it and by consequence it's entities created or not.
You can find an example in Sonata bundles, you can manage the users as you want, but if you are using FOSUserBundle, the you have the option to install SonataUserBundle, then tell to fos_user configuration that the new class belong to the Sonata User and as consequence the new entity will be persisted with a lot of new attributes thanks to class inheritance, and all the crud operations for user will be already configured in sonata views. SonataUser also have it's own user entity for using in a standalone way.
I know that this is not what you asking for but may be you just need manage to follow a model like this.

Where shall I put my business logic without mapping to a database in Symfony2?

I created a bundle in Symfony2 that contains my webapp. In there I have some Entities that are mapped to my MySQL-Database. They are located in Acme\MyBundle\Entities.
Now I have created some helper classes that I will not just use in MyBundle, so I outsourced them into a separate bundle (e.g. Acme\MyHelperBundle). Everything is clear until here.
But where exactly do I put business logic files? Do I put them into
Acme\MyHelperBundle\Entities or
Acme\MyHelperBundle\Resources or
Acme\MyHelperBundle\Resources\src
or any other location?
And what would be a good namespace for these helper classes?
create a model folder and in that make a class where you put the business logic and create a service of that class and used that classes anywhere in the project.
I think there's no real rules for that.
if your helper class are declared as services (i think so if you want to use them in all your bundle) you can create a Manager folder. So when i'm on a bundle and there is a Manager, i know that i have some logic that i can reuse everywhere.
It's depend, too, on what you put i your files, but if it's some helper like a UserManager.php on which you create some new request (And you declare it as a service), the Manager folder it's a good place to start.
You can find this folder Manager in many bundle like fosElasticaBundle
ps: sorry for my english, i hope it's clear enough:)

How can I implement multi-tenant translations in Symfony2

I am developing a multi-tenant capable Symfony2 solution and was wondering if there was a way to use different translations files for each tenant, as the default translations files at present contain e.g. references to the initial tenant's company name, etc.
I am using the Liip Theme Bundle (https://github.com/liip/LiipThemeBundle) to allow tenants to use our codebase, layering their own design on top, but cannot work out a simple and scalable way to allow them to use their own translations files.
There was talk on the theme bundle git repo about this, but I don't believe anything was ever implemented (https://github.com/liip/LiipThemeBundle/issues/12). Ideally I'd like to follow the directory structure they suggested in that thread, e.g.
root
- app
- Resources
- themes
- <theme name>
- public
- translations (this would be new)
- views
as this would allow us to continue the practice of themes being self-contained git submodules that a tenant can maintain themselves.
I ended up using the directory structure I outlined above, and had a console command which symlinked the translations override file in app/Resources/translations. This command ran during my deploy script, and I then created my own "trans" twig function which checked if an override file should be used.
Not the cleanest, but definitely works the way I wanted.

Resources