I am trying to get along with translation service in symfony2. The problem I am facing is that this code always returns the english version of the key
$translator = $this->get('translator');
$translator->setlocale('fr'); // locale gets ignored
die($translator->trans('pisica')); //always outputs cat altough the translation in french exists too.
I would like to specify in controller witch locale I am using because it is best for what I am trying to acomplish
Set the Locale in the Request object, not in the translator service.
$request->setLocale('fr');
$translator->trans('something');
Related
is there any way to switch language in nextjs without passing language parameter in url like baseurl/ar or baseurl/en ? if I want to change language from dropdown, url should not change.
query parameters are defined by the routes of your api, in this way, when you change the url, a new call is made in the api passing the parameters that were informed in the route.
you can pass objects to the api route, something like:
const response = awai api.get('yourUrl', {language: en})
this way the information will not appear in the url, but this needs to be changed in the backend so that it knows where to get the parameters from.
you can choose to do the translation in json files, and just switch between them too.
Hope this helps.
#NSL
I am trying to use BizTak WCF-WebHttp adapter to send to Service Desk Plus CMDB API using Variable Mapping.
When trying using the browser, it works fine. Service Desk Plus CMDB API requires an URI like (strictly shortened for readability):
http://host.com/api/cmdb/ci?OPERATION_NAME=read&TECHNICIAN_KEY=Mykey&format=XML&INPUT_DATA=<?xml version='1.0'?>
<API>
<name>email#host.com</name>
</API>
I have used the URI http://host.com/api/cmdb/ci and URL Mapping.
<BtsHttpUrlMapping>
<Operation Url="?OPERATION_NAME=read&TECHNICIAN_KEY=MyKey&format=XML&INPUT_DATA=<?xml version='1.0'?>
<API>
<name>email#host.com</name>
</API>"/>
</BtsHttpUrlMapping>
This works fine, but I need a more dynamic approach. I tried using Variable Mapping, so I replaced the hard coded email address with a variable.
<BtsHttpUrlMapping>
<Operation Url="?OPERATION_NAME=read&TECHNICIAN_KEY=MyKey&format=XML&INPUT_DATA=<?xml version='1.0'?>
<API>
<name>{email}</name>
</API>"/>
</BtsHttpUrlMapping>
Trying to save the URL Mapping with the variable I get an error.
WCF-WebHttp Transport Properties
Error saving properties.
(System.InvalidOperationException) The UriTemplate
?OPERATION_NAME=read&TECHNICIAN_KEY=MyKey&format=XML&INPUT_DATA=<?xml version='1.0'?><API><name>{email}</name></API>
is not valid; each portion of the query string must be of the form 'name=value', when value cannot be a compound segment. See the documentation for UriTemplate for more details.
If I try a variable that is not within the escaped XML string, like with the key, then it works fine.
<BtsHttpUrlMapping>
<Operation Url="?OPERATION_NAME=read&TECHNICIAN_KEY={key}&format=XML&INPUT_DATA=<?xml version='1.0'?>
<API>
<value>email#host.com</value>
</API>"/>
</BtsHttpUrlMapping>
My intention is to be able to use a variable within the escaped XML string. If that is not possible; I will have to turn to a dynamic adapter and Create the URI and URL mapping in an orchestration.
Did u understand why it said each portion of the query string must be of the form 'name=value? There are just a few ways to make UriTemplates work.
See how a UriTemplate works here. Here is an example that is valid:
weather/{state}/{city}?forecast={day}
So in your case you should make everything after INPUT_DATA= a variable. Which means the whole escaped XML string you were talking about.
I am working a multi-lang site. I want to get and set culture and uiculture with culturename without countryname. If browser is english or lang is English choosen, it will return en not en-US or en-GB. Because I use one localresources file per language and I have to send language code to sql procedure as a parameter.
Thread.CurrentThread.CurrentCulture
Thread.CurrentThread.CurrentCulture.Name
Thread.CurrentThread.CurrentUICulture
All of them returns en-GB,de-DE,de-AT etc... I just want first part and use this ones.
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28VS.71%29.aspx
There are the name in the link but browser does note return it, I just want and use the simple part.
How can I do that?
It is solved (:
edit:
Now, How can I read browser's culture and if I don't have it, how can I set the culture that I have.
eg: I have en,de,ru and the visitor's browser sen me fr, I want it is shown en ?
Have a look at the TwoLetterISOLanguageName-property. It should return the identifier you are looking for. For example:
var cult = new System.Globalization.CultureInfo("en-US").TwoLetterISOLanguageName;
Returns "en" so that you can send it to the stored procedures.
As the title suggests, I'm using Symfony in conjunction with the JMSTranslationBundle and JMSI18nBundle in order to serve translated routes.
Here's my currently configured route:
/{location}/{profession}/{specialty}
So the route
/berlin/arzt/allgemein
is successfully pushed to the correct controller and action.
The JMSI18nBundle is automagically prefixing my English routes with /en/. This works for every other route with a non-dynamic component (such as /profile/{slug}/). This DOES NOT work, however, when using the English version of the above example. i.e.
/en/berlin/doctor/general
I'm guessing the router is not reading this properly as the English version of the normal route, and instead tries to assign location = en, profession = berlin, etc, which is obviously incorrect.
I've tried defining optional parameters, more complicated regexes, and trailing slashes for the translation (all with cache flushes in between). None of this works. What DOES work, is inserting a pointless non-dynamic component, i.e. /en/s/berlin/doctor/general etc
As a part of the business requirements, we don't want this additional pointless non-dynamic URL component.
So, my question is: how can I use (prefixed) translatable URLs in Symfony that contain nothing but dynamic fields?
Your help is greatly appreciated!
Solved:
As is the norm with Friday-afternoon problems, I found I had a $ inside my translated route rule, like so:
/{location}/{$profession}/{specialty}
Removing it and flushing the cache resulted in the route working.
tl;dr - PEBKAC
When you use #Assert\NotBlank constraint and the given field is empty, then you get the error:
This value should not be blank
I would like to change this message application-wide, without changing Symfony2 source code. How to accomplish that?
Cutomizing validation error messages is quite simple, but can seem tricky at first.
Default locale
First of all you should change the default locale of your application. In versions 2.0.x the correct value to change is framework.session.default_locale. For future reference, starting from 2.1.0 it'll be framework.default_locale. Consult the docs for correct syntax.
A locale should consist of your language and region and is defined as language_REGION (list of languages, list of countries). The locale used in Germany for german would be de_DE for example.
Validation messages
Validation messages are hard coded in their respective constraint classes.
Translating validation messages
Symfony uses Twig to render all the validation messages. The process itself is complicated and falls out of the scope of this question, but the important part is that each constraint message is sent through a translation filter, which depending on the user's locale (default_locale by default) translates the messages to the proper language.
To change any of the translations, simply copy the validation translation file from vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/validators.{lang}.xlf to app/Resources/translations/validators.{lang}.xlf where {lang} is the language part of your default locale.
Having done the former, simply change the default messages to what ever you see more fit. If the language you need support for doesn't exist, copy any translation file to the same directory and modify that file instead.
To read more about how translation works in Symfony2, visit the official documentation on translation.
Additionally to the instructions by gilden, you have to make sure the framework.translator block in config/config.yml is uncommented (it's commented by default nowadays). If you don't do that, you'll still end up with the original English messages.
If anyone has doubts in 2021, here's all I had to do to switch "Error" (eng) constraint message to "Erreur" (fr):
Go to your config/packages/translation.yaml file
Edit it by replacing "en" to "fr" (or your language) :
Test it on your localhost :
Note :
here is the Symfony 5.2 doc I used to help me out
All I did was set a Default_locale (!! not detect where the user comes from !!)