I want to devide translations into diffrent files, by default the translation domain messages is used. Inspired by easyadmin translations I used a helper function to set the domain automatically.
function t(string $message, array $parameters = []): TranslatableMessage
{
return new TranslatableMessage($message, $parameters, $domain='admin');
}
When exporting the translations from code, only the domain messages work, but not the domain admin, although it was defined in the helper function:
php bin/console translation:extract --force --format=yaml --domain=admin en
Result: [WARNING] No translation messages were found. and the file admin+intl-icu.en.yaml was not created.
php bin/console translation:extract --force --format=yaml --domain=messages en
Result: translations are exported into messages+intl-icu.en.yaml
Allthough all translations use the specific domain, it was not found when exporting.
How to define a translation domain for a certain scope, so that translations are exported into the correct domain? Your help is highly appreicated.
Related
apparently, Symfony cast env variable is not working on symfony 4.0.
I have this configuration:
cache:
session:
enabled: "%env(bool:SESSION_CACHE_ENABLED)%"
But I get this error:
Invalid type for path "cache.session.enabled". Expected boolean, but got string.
What is my problem? I'm using symfony version 4.0
Thanks
EDIT
Probably, it is a problem of the plugins. That's what I think: Symfony 4 is now based on .env config variable, that are STRING as default; to handle this, S4 is able to use "casting" env var
'%env(bool:myvar)%'
And it works; if you do a var_dump within a controller, you can see that the variable is a boolean.
Most of current plugins, also those who supports S4, are not able to use this syntax, so, they see that variable as STRING, and the validator return an error.
These plugins should be fixed or, actually, I can duplicate the .yml file on each package/{env}/ dir with separated configuration ( the situation that I would avoid with .env )
The problem is fixed in symfony 4.1 https://github.com/symfony/symfony/issues/22151
Have looked through SO at various Symfony routing issues but no-one seems to have the same issue as this.
Yesterday, routing worked without issue.
Today I am getting errors regarding values being required for arguments with defaults set
This is an example route that is causing me a problem
#Route("/summary/{staffId}", name="task_instance_summary", requirements={"staffId":"\d+"},defaults={"staffId":"0"})
The method definition:
public function summaryAction(Request $request, $staffId)
and of course the error:
"Controller "PlanXL\TaskBundle\Controller\InstanceController::summaryAction()" requires that you provide a value for the "$staffId" argument (because there is no default value or because there is a non optional argument after this one).")
Debug output:
[router] Route "task_instance_summary"
Name task_instance_summary
Path /task/instance/summary/{staffId}
Path Regex #^/task/instance/summary(?:/(?P\d+))?$#s
Host ANY
Host Regex
Scheme ANY
Method ANY
Class Symfony\Component\Routing\Route
Defaults _controller: PlanXLTaskBundle:Instance:summary
staffId: 0
Requirements staffId: \d+
Options compiler_class: Symfony\Component\Routing\RouteCompiler
I have already cleared the cache (even though I am working on dev) but I can't see why I am getting an error. The router can obviously see the default value when I run the debug so why not when called through my application?
Try it;
public function summaryAction(Request $request, $staffId = 0)
I have a symfony2 project running with nginx and the problem is that when accessing the prod environment, all the links are still with app_dev.php there.
Here are my config files:
app.php
<?php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
use Symfony\Component\HttpFoundation\Request;
$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
$kernel->handle(Request::createFromGlobals())->send();
The links in the twig file are called this way:
Route name: user_login, in twig: user_login_path()
Where could the problem come from? Nginx?
You should always use path('route_name') in order to generate links or actions. It seems that the function(s) you are using (kind of twig extension) is bad coded and server dependent. But it does not comes with Symfony itself.
You have multiple options :
The cleanest : replace every href, action, ... with the path function
A (maybe) faster one : find the custom twig extension and make it work (more likely in src/Acme/MyBundle/Twig/MyExtension.php)
A quick and dirty one : rewrite every app_dev.php urls to app.dev with a .htaccess (not really recommended at all but... well...)
I'm trying to use the JMSI18nRoutingBundle to translate my routes. I've installed and configured it like it's shown in the documentation :
composer.json:
"jms/i18n-routing-bundle": "1.1.*#dev",
AppKernel.php:
new JMS\I18nRoutingBundle\JMSI18nRoutingBundle(),
new JMS\TranslationBundle\JMSTranslationBundle(),
And config.yml:
jms_i18n_routing:
default_locale: %locale%
locales: [en, fr]
catalogue: routes
strategy: custom
Then I'm trying to extract the translations for the routes. The command given in the doc
php app/console translation:extract fr --enable-extractor=jms_i18n_routing
doesn't work at all, but I found this command on internet that works better:
php app/console translation:extract fr --enable-extractor=jms_i18n_routing --bundle="TeduleCoreBundle" --domain="routes"
With this, the translations are extracted in a routes.fr.yml file in AcmeCoreBundle/Resources/translations. The file contains the translations for all the routes in all the locales, instead of only the routes for the AcmeCoreBundle in the fr locale like specified in the command, but oh well, it works...
However, when I do a php app/console router:debug, none of the routes are translated. Instead, I end up with something like this:
en_fr__RG__tedule_app_account_profile ANY ANY ANY /myaccount
en__RG__tedule_app_account_profile ANY ANY ANY /myaccount
fr__RG__tedule_app_account_profile ANY ANY ANY /myaccount
en_fr__RG__tedule_app_account_notifications ANY ANY ANY /myaccount/notifications
en__RG__tedule_app_account_notifications ANY ANY ANY /myaccount/notifications
fr__RG__tedule_app_account_notifications ANY ANY ANY /myaccount/notifications
en_fr__RG__tedule_app_account_password ANY ANY ANY /myaccount/password
en__RG__tedule_app_account_password ANY ANY ANY /myaccount/password
fr__RG__tedule_app_account_password ANY ANY ANY /myaccount/password
As you can see, each route appears 3 times (en_fr, en and fr), and the pattern isn't translated according to what I wrote in the routes.fr.yml file.
I tried clearing the cache, putting the routes.fr.yml file in app/Resources/translations, separating the translations between two files routes.fr.yml and routes.en.yml, nothing works...
That happens to me too but I solved it by moving the translation files of the routes to the "app/Resources/translations"
This works to me:
php app/console translation:extract en_US --bundle=MyBundle --enable-extractor=jms_i18n_routing --output-format=xliff --domain=routes --keep --output-dir=app/Resources/translations
Hopefully a simple question - how does one specify which environment to use when running a console command in Symfony2. I've created a few commands, however I would like to run them in the context of my 'staging' environment when on my staging server and my 'prod' environment when on my production server (the different environments define different database connections). How do I configure and pass this information to my console command?
You have two options that will help you out. You can specify the environment that the console will run in with the --env flag, and use --no-debug to disable debug mode.
php app/console --env=staging your:console:command or php app/console --env=prod your:console:command should do what you're looking for (the console runs in the dev environment with debug on by default).
You can look at the code of the app/console file for more info.
You can also use the SYMFONY_ENV environment variable to set a default environment distinct to 'dev' (e.g. export SYMFONY_ENV=prod in ~/.bash_profile)
To answer the question #croca had, to expand on what #Francesc Rosàs posted, and as #Problematic suggested.
If you look in app/console you should see $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
All this does is checks the input arguments passed to the console for --env or -e, checks the default value from getenv('SYMFONY_ENV'), or sets it to dev if neither are supplied.
It is then passed to $kernel = new AppKernel($env, $debug);
You could essentially either make changes directly to app/console to achieve your application's specific functionality or copy app/console to a separate file such as app/exec, then process the $env variable how you prefer to determine the desired environment.
Simple Example: app/exec
#!/usr/bin/env php
<?php
/**
* disabled debug mode,
* set environment based on requesting address being local
* otherwise the server's IP address
* be sure to perform at least chmod(750) on the file to ensure it is executable
* otherwise it must be prefixed with your php executable
*/
set_time_limit(0);
require_once __DIR__.'/bootstrap.php.cache';
require_once __DIR__.'/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = ($_SERVER['REMOTE_ADDR'] === '127.0.0.1' || $_SERVER['REMOTE_ADDR'] === '::1' ? 'dev' :
($_SERVER['SERVER_ADDR'] === '192.168.2.1' ? 'test' : 'prod')
);
$kernel = new AppKernel($env, false);
$application = new Application($kernel);
$application->run($input);
Then call php app/exec namespace:command arguments --flags
Additionally you could process your own application instead of using the app/console AppKernel - instructions from Symfony can be found here: http://symfony.com/doc/current/components/console/introduction.html
The official documentation says:
By default, console commands run in the dev environment and you may want to change this for some commands.
So, as #Problematic suggest, you can specify the environment for your command using the flag "--env=your_env" or its shortcut "-e your_env".
I wonder whether it is possible to force the default environment for a command. I mean, without having to use the flags. Any idea?
There is another variant, not so convenient though, but works also with composer
SYMFONY_ENV=prod app/console cache:clear
Maybe you could look at the implementation of the cache:clear command. This may give you some clues on how to choose the environment from a command.
app/console help cache:clear