I am just starting using symfony2 and facing some difficulty need helps from expert like you guys.
Question 1: How to set the default locale and fallback locale so that people try to hit something like www.example.com/home or www.example.com/fr/home (not supported locale) will redirect to www.example.com/cn/home?
I have read a lot from symfony2 document (http://symfony.com/doc/current/book/translation.html) and also searching on google but I still can't make it works. Not only default locale not working, the fallback locale also not working. For example, when user try to enter www.example.com/fr/home which is not supported by my app, it supposes to redirect the user to www.example.com/cn/home but it fails to do so.
I try to clear cache also but it seem like not working.
Question 2: How to match the translation key in case insensitive?
For example, in my translation file ("message.cn.yml") contains
Welcome: 欢迎
So in my twig template, I use {{ 'Welcome' | trans }} it will help me convert to 欢迎 but when I use {{ 'welcome' | trans }} it will not convert. Is there any ways to make it to case insensitive so I do not need to purposely add in the key "welcome: 欢迎" to the message.cn.yml file.
Your help and suggestion is very much appreciated. Thank you.
Below are my app files for your reference:
Translations file:
Parameters file:
Config file:
Routing file in the particular bundle("Acme/DemoBundle"):
Twig file:
Here is the result when I hit
localhost/locale/web/app_dev.php/
localhost/locale/web/app_dev.php/en
localhost/locale/web/app_dev.php/cn
localhost/locale/web/app_dev.php/fr
You can set locale for a request:
$this->container->get('request_stack')->getCurrentRequest()->setLocale($locale);
and you can set locale per request using a request listener:
http://symfony.com/doc/current/cookbook/service_container/event_listener.html#request-events-checking-types
for your second question you can add conversion to lower case:
{{ 'WELCOME'|lower | translate}}
or you can create your own customized twig extension that is doing the translation and other stuff in between:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
Related
I have been researching for almost 2 weeks on for an answer and have finally decided I needed to ask for help. I am quite unfamiliar with PHP/Drupal but has inherited such a project to update and maintain. The project runs on Drupal 8.3.7 and is hosted with IIS 6.2.
For some unknown reason, links in my html.twig files are being returned with a "null" base_url. So for example...
...
will link to "null/home" ... After a lot of hacking and trying to figure out if I could manually set $base_url (not a best practice, I know, but I just needed to see if it was possible), I found two peculiar behaviors:
A. If in the associated controller I create a variable $temp_base_url and hardcode the value "localhost" and attempt to put the following in my html.twig file (where item.url = "/home")...
http://{{temp_base_url}}{{item.url}}
The text between the anchor tag will show up correctly as http://localhost/home while the actual link will direct you to null/home .
B. On the other hand, if I assigned $temp_base_url to equal "http://localhost" and put the following code...
{{temp_base_url}}{{item.url}}
The text between the anchor tag will (aga) show up correctly as http://localhost/home while the actual link will now try to direct you to http://localhost/null/home .
The problem isn't with the global variable $base_url because when I test for this within the Controller, $base_url is setting itself correctly to "localhost" so I suspect Symfony or something else rendering the routes incorrectly (any attempts to use 'path' or 'url' has also resulted in similar null issues).
So my question is: Where are these nulls coming from? How do I get IIS/Drupal to pick up the appropriate base url for routing?
You can use this url('<front>')
{{ url('<front>') }}{{item.url}}
We have received below errors on the home page:
eZTemplate # design/dffestival/templates/page_footer.tpl:8[6]:
Unknown template variable 'view_parameters' in namespace ''
In our pagefooter.tpl file have below code:
<div class="attribute-layout">
{attribute_view_gui attribute=$footerNode.data_map.layout view_parameters=$view_parameters}
</div>
We are using eZ Publish Community Project 2012.6 version.
Could anyone explain why I can't retrieve the view_parameters variable and how to do retrieve it?
Thanks
Sunil
Maybe u should start here:
http://www.ezpedia.org/ez/view_parameters
Remember view parameters are by default available only within the
context of the content module and it's views.
All other modules (by default) do not support this feature.
The recommended alternative to view parameters in these situations
would be using get / post parameters instead.
Or here:
https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Templates/Basic-template-tasks/Custom-view-parameters
In some cases $view_parameters won't work, try
$module_result.view_parameters there. So the example above will be:
The color is: {$module_result.view_parameters.color} The amount
is: {$module_result.view_parameters.amount}
Or here (check "$module_result section"):
https://doc.ez.no/eZ-Publish/Technical-manual/3.10/Templates/The-pagelayout/Variables-in-pagelayout
Short answer :
You are trying to retrieve the $view_parameters within the pagelayout (or a template included in it). This is not possible by design and this is completely normal.
Long answer :
View parameters are meant to be used from the views/templates which are used by the content module : for instance, when viewing a content from its alias URL or using its system URL like /content/view/full/2.
They are useful if you want to pass some parameters from the URL to the content view and they are taken into account by the cache system which is a very important thing to keep in mind (this is not the case when using "vanilla" GET parameters).
The main usage is for pagination, for instance : /content/view/full/2/(offest)/2/(limit)10
One of the best practices when developing with eZ Publish (legacy) is to ask yourself : why do you need to retrieve these parameters into your layout ? I guess that you want to control your global layout using them and this is not a good idea.
If you want to control the layout based on something which depends on the content, then I'll suggest to use persistent variables. You will basically use the ezpagedata_set operator in the content/view template and retrieve this value in your pagelayout with ezpagedata() | see https://doc.ez.no/doc_hidden/eZ-Publish/Technical-manual/4.x/Reference/Template-operators/Miscellaneous/ezpagedata_set
Last but not least, remember that the module result is computed before the pagelayout (simply because the pagelayout will include this result using $module_result.content).
I am using Yii framework to develop a multilingual web site. Right now I have English and French versions. I have created a fr folder and placed views and controllers related to French version, but using same models for both versions.
My database tables structure (articles table) is:
id, title_en, title_fr, title_ru, detail_en, detail_fr, detail_ru and
so on.
The issue I am facing is that I need to display error messages to the users in respective languages like in French version error message should be in French. How can I do while using same model for all languages.
Where do I need to put the French messages?
I don't know, if I'm getting you correct, but if I understand your question, your problem is already solved by core Yii 1.x engine and there isn't much work left to do.
In detail, you have to:
Create any file (for example app.php) in /protected/messages/fr/ folder.
Put 'coreMessages'=>array('basePath'=>'../protected/messages'), to your application's configuration array -- route Yii translation system to folder with your customized translations.
Put 'sourceLanguage'=>'en' to your configuration array -- make sure, that your applications's core language is set to English.
Fill app.php file with your translation strings (see below).
Translate parts of your application with Yii::t('app', 'string'); (see below).
Your translation files should be in format:
return array
(
//Translations from Google Translate! :> I don't speak French at all! :>
'This page is not yet translated into your language.'=>'Cette page n'a pas encore été traduit dans votre langue.',
'Language'=>'Langue',
'read more'=>'en savoir plus'
);
So, for example, but in your blog list view string like:
<?php Yii::t('app', 'read more'); ?>
When you set your application language to fr, either by forcing it in code (Yii::app()->language = 'fr';) or presetting it in appliction's configuration array ('language'=>'fr'), you should see en savoir plus (or whatever you put to your translation file) in place of read more in above view.
If your application's language is set back to English or if given translation string is not found, you'll see text written in Yii::t. That's why it is so important to use fully qualified English strings in this method (and in translation files) and to avoid "patterns", like 'core.app.language'=>'Langue' etc.
Note, that you can also use parameter placeholders:
'Error no {code}:'=>'Erreur n {code}:'
that will be replaced with the actual parameter values:
Yii::t('app', 'Error no {code}:', array('{code}'=>$code));
This way you can provide variable values to otherwise constant message text.
There's a great topic about internationalization in "The Definitive Guide to Yii". You should read it through for more information on this matter or, if you want to use Yii translation mechanism in a bit more advanced way.
You can override CPhpMessageSource::loadMessages() with something like following:
protected function loadMessages($category,$language)
{
if ($category == 'yii')
return array();
else
return parent::loadMessages($category,$language);
}
Also You can change the language, set CApplication::language appropriately. This can be done at runtime as in
Yii::app()->language = 'fr';
I'm trying to get CakePHP's i18n component to work. I have extracted my strings to app/Locale/default.pot using the i18n console task. I then copied it into app/Locale/eng/LC_MESSAGES/default.po and app/Locale/fra/LC_MESSAGES/default.po making sure to change the extension. I used the program Virtaal (similar to Poedit) to translate some of the strings.
In my app/Config/core.php I have set my default language to english with Configure::write('Config.language', 'eng'); if I change it to Configure::write('Config.language', 'fra'); I expected to see the new translated strings but nothing changed. I tried setting the Config.language key in the session as well but it didn't do anything. Printing out the configure value and session values I can see they are being set.
Am I missing something here? also in the many different posts I've been reading about i18n in CakePHP I've seen the key fre being used interchangeably with fra is there a difference?
After http://book.cakephp.org/1.3/en/The-Manual/Common-Tasks-With-CakePHP/Internationalization-Localization.html it should be fre for french.
// locale path
/app/locale/fre/LC_MESSAGES/default.po (French)
// To change or set the language for your application, all you need to do is the following:
Configure::write('Config.language', 'fre');
// To set the language for the current user, store the setting in the Session object, like this:
$this->Session->write('Config.language', 'fre');
Further than that:
In our cakePHP application I have to restart the apache webserver after changing the files to get the new strings because of caching. Perhaps you have to do that too, but I'm not quite sure as we generate .mo files out of the .po via an POEdit setting. I dont't think you are forced to do this, because the cookboke doesn't say anything about that (or I didn't found it now :D ).
edit:
Looks like cake is using the bibliographic codes for french but the terminology for german. That's very confusing :/ : http://www.loc.gov/standards/iso639-2/php/code_list.php
How can I get the current URL from a Twig template?
I am using Twig with PHP, without any other framework.
The following works in Silex and most certainly in Symfony2 as they share the Request class (I did not test though) :
{{ app.request.getRequestUri() }}
Finding the current URL
The current URL is supplied by your web server and written to the $_SERVER super-global. Run this small script, <?php echo '<pre>'; print_r($_SERVER);, through your server and root around to find the value(s) you are looking for.
Related questions on this subject:
getting current URL
PHP Determining the current url
How to get URL of current page in PHP
The PHP manual describes the nature of the available $_SERVER values here.
Getting the URL in TWIG
After you have the URL, you need to pass it as a template variable when calling render(...) on the Twig template instance. For example, you might code this.
$current_url = // figure out what the current url is
// pass the current URL as a variable to the template
echo $template->render(array('current_url' => $current_url));
To use the variable in the template, you use the {{ variable_name }} syntax.
Developer's basic Twig documentation
Twig designer documentation for using variables.
Go http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html
or : {{ app.request.getUri() }} for full Uri.
Keeping best practice in mind, at this time you should use Symfony\Component\HttpFoundation\RequestStack.
See http://symfony.com/blog/new-in-symfony-2-4-the-request-stack.
As of Symfony 2.4, the best practice is to never inject the request service, but to inject the request_stack service instead [...]
So in a Silex application it could be achieved with :
app.request_stack.getCurrentRequest.getUri
Here something I found to make it generic with the sliex Framework.
I guess my solution is not perfect but it's get the job done.
in your PHP code add this code :
$app = new Silex\Application();
// add the current url to the app object.
$app['current_url'] = $_SERVER['REQUEST_URI'];
Then in your Twig template you can do
{{ app.current_url }}
Let me know what is the botom line of this method.