I have two translation languages: en and ru
I have locale: en|ru in parameters.yml and translator: { fallbacks: "%locale%"} in config.yml.
I need to do switching of languages. I need to save locales into session and set the right locale in parameters.yml. How i change locale parameter value to the right one from session?
The parameters.yml is not designed for dynamic content, but I think what you want to you need is this:
To get the currently set locale:
$locale = $request->getLocale();
To set the preferred locale order:
$session = $this->get('session');
$session->setLocale($request->getPreferredLanguage(array('de', 'en')));
Related
This may be a silly question, but i can't see how to access this data :
In the main app/config/config.yml, i have the general configuration data for my application.
parameters: #accessible by getParameter()
locale: fr
# ...
fos_user: #accessible by ???
#...
registration:
form:
from_email:
address: mymail#mydomain.fr
sender_name: TheSenderName
In my custom bundle i can access the parameters from that config.yml file with :
$this->container->getParameter('locale'); //gives me "fr" as expected
But how can i access the non parameters configuration values ?
i would like here to get the adress defined in te FOS User bundle configuration :
I can't do
$admin_adress = $this->container->getParameter('fos_user.registration.confirmation.from_email.address');
What is the right way to access thoses ?
Edit :
Yes i wanted to access this config data from FOS User Bundle (here in the example, but it could be any), inside a controller or whatever in my bundle.
I think this question nailed it
class MyProjectExtension extends Extension
{
public function load( array $configs, ContainerBuilder $container )
{
// The next 2 lines are pretty common to all Extension templates.
$configuration = new Configuration();
$processedConfig = $this->processConfiguration( $configuration, $configs );
// This is the KEY TO YOUR ANSWER
$container->setParameter( 'from_email.address', $processedConfig[ 'registration.confirmation.from_email.address' ];
// Other stuff like loading services.yml
}
You can act that way
parameters:
locale: fr
fos_user:
registration:
form:
from_email:
address: mymail#mydomain.fr
# ...
fos_user:
#...
registration:
form:
from_email:
address: %fos_user.registration.form.from_email.address%
sender_name: TheSenderName
Of course I've choose this name just to fit your controller request for parameter: you can (and maybe should) choose other keys.
However with container->getParameter you can access only parameters section of your configuration file (plus parameters exposed from vendors)
One of the solutions can be passing your controller as a service, then inject your parameters like that :
# services.yml
my.amazing.controller:
class: CompanyFirst\LabBundle\Controller\PloufController
arguments:
- '#filesystem'
- '%dump_file_convention%' // your config parameter
I am using mentioned bundle in my application, and I would like to be able to configure it this way:
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: custom
hosts:
en: [mydomain.com, subdomain.domain.com]
de: mydomain.de
redirect_to_host: false
so multiple domains to one locale. I would like to run two similiar websites at one application to have access to the 90% of the code which is similiar and same database. Any tips how could i achieve this? Or maybe there is other bundle/solution more accurate for my problem?
From the configuration you cannot bind multiple domains to one locale.
You can try to extend this class of the bundle:
JMS\I18nRoutingBundle\Router\DefaultLocaleResolver
You need to change this part:
public function resolveLocale(Request $request, array $availableLocales)
{
if ($this->hostMap && isset($this->hostMap[$host = $request->getHost()])) {
return $this->hostMap[$host];
}
...
}
adding a more complex hostMap that supports multiple domains for the same locale.
I'm using a2lix_translation_form tabs in my form. It has the feature, that it allows you to edit several translations to one property in one form. I have it configured like this:
a2lix_translation_form:
locales: [sk, en, de] # [1]
default_required: false ... # further as default
In the form I have following 3 Tabs where I can edit one property (Description)
|SK [Default] | En | DE |
It worked fine (stored things in database and so on), until I turned on the translatable in stof_doctrine_extensions. Here is the config:
stof_doctrine_extensions:
default_locale: sk
orm:
default:
translatable: true # not needed: listeners are not enabled by default
I also use jms_i18n_routing:
jms_i18n_routing:
default_locale: sk
locales: [sk, de, en]
strategy: prefix_except_default
When I acess
localhost/app_dev.php/product/1/edit
then everything looks fine, but when I access
localhost/en/app_dev.php/en/company/11/edit
the Sk [Default] contains En description.
When I set the translatable in stof_doctrine_extensions to false the form is displayed correctly. But I need to have it ON, because I need it for other components. What can I do?
You are in a specific case, that I don't advice henceforth. You will have some difficulties with you database if you change your default locale in the future.
I've updated the doc (I have still some work..), see the end of http://a2lix.fr/bundles/translation-form/#bundle-advanced.
You can use annotation as explaned in the doc or add at the beginning of yours edit/create methods:
$translatableListener = $this->get('stof_doctrine_extensions.listener.translatable');
$translatableListener->setTranslatableLocale($translatableListener->getDefaultLocale());
My local works everywhere on my website but I cant tell the application the set the proper local on the root of the application.
For exemple :
If my website is http://mycompany.com I want that, when I enter this address the application guess what is my local and set it at the end of the url
http://mycompany.com/en if the local exist on my application. Here en or fr and if not the default en
For now when i go to home page I always get english version but my browser is set in french
routing.yml :
_welcome:
pattern: /{_locale}
defaults: { _controller: MyBundle:Default:landing, _locale: en }
requirements:
_locale: en|fr
See this question asked a few days ago and take a look at the first method in the language listener. It does exactly what you need. And take care of the right settings in config.yml
With the help of many other questions related to locale and symfony2 I finally get want I want ! I guess it is not the perfect answer but at least it works !
My landing action is defined in my routing as _welcome route. It is this action that is loaded when I go to the url's root.
/**
* #Route("/landing")
* #Template()
*/
public function landingAction() {
$localeAvailable = array('en', 'fr');
//user language
$localeUser = substr($this->getRequest()->getPreferredLanguage(), 0, 2);
//application language
$localeApp = $this->getRequest()->attributes->get('_locale');
//Redirect to the good locale page
//only if the locale user is on our manager locale and it is not the current locale of the application
if(in_array($localeUser, $localeAvailable) && $localeApp!=$localeUser){
return $this->redirect($this->generateUrl("_welcome", array('_locale' => $localeUser)));
}
return array();
}
I'm having a strange issue with Symfony2.2. I have a project using two languages : en/fr. So I create as usual (like Symfony2.0) two translation files "messages.en.yml" and "messages.fr.yml" in Ressources/Views/translations/. But Translations in twig could not change even if we set the request object and the locale session. Translation is always set by the default_locale (config.php).
Example : if default_locale = en, all my website (in twig) is translated in en, even if i set the _locale object in fr (request and session). Of course if I manually change the default_locale to fr the website is naturally in fr...
However, _locale session works but I don't know if locale request works, and of course translation works in controllers too...
There is my files :
config.yml:
framework:
#esi: ~
translator: { fallback: %locale% } # = en
# ...
default_locale: %locale% # = en
Controller :
public function indexAction()
{
$this->get('session')->set('_locale', 'fr');
$this->getRequest()->setLocale($lang);
exit($this->getRequest()->getLocale()); // = fr
exit($this->get('translator')->trans('Symfony2 is great')); // = Symfony2 est génial
return $this->render('TestBundle:Controller:test.html.twig');
View :
{% block content %}
<p>lang : {{ app.request.locale }}</p> {#} = "fr", OK{#}
<p>{{ 'Symfony2 is great'|trans }}</p> {#} = "Symfony2 is great", WAIT WHAT?{#}
I must resign myself to force the locale at the beginning of the method controller to have the requested locale (stored in session) like that :
Controller:
if($this->get('session')->get('_locale')){
$lang = $this->get('session')->get('_locale');
$this->getRequest()->setLocale($lang);
}
In other words, I do have a problem with the registration of the request object... Because the last code works well in the controller, and shows well the locale in twig page with app.request.locale, but not the translations... (sorry for my bad english and thanks for helping)
I had the same issue due to the low priority of my event listener. The locale would be overridden by the Translator's TranslatorListener. Increasing the priority of my event listener did the trick for me:
services:
app.locale_listener:
class: AppBundle\EventListener\LocaleListener
tags:
- { name: kernel.event_listener, priority: 11, ... }
Source: https://github.com/symfony/symfony/issues/12878#issuecomment-68628808
Parameter _locale in routing holds your locale value.
Look here on this page
Symfony - Book - Translation - Local and the URL
From Symfony 2.1 they have this kind of logic:
Since you can store the locale of the user in the session, it may be tempting to use the same URL to display a resource in many different languages based on the user's locale. For example, http://www.example.com/contact could show content in English for one user and French for another user. Unfortunately, this violates a fundamental rule of the Web: that a particular URL returns the same resource regardless of the user. To further muddy the problem, which version of the content would be indexed by search engines?
A better policy is to include the locale in the URL. This is fully-supported by the routing system using the special _locale parameter:
Now when you want to sel local, this doesn't work any more
$this->get('session')->set('_locale', 'fr');
You can use request insted of session now but you can not have session-logic with _local from Symfony 2.0 unless you simulate it with event listener on kernel request.