Basics of I18n For Yii Framework - php

Yii's I18n topic isn't enough for me.
My source lang is Turkish , target lang is English (for example)
My test controller's index action :
public function actionIndex()
{
Yii::app()->language='en';
$this->render("index");
}
This is my view file's content :
echo Yii::t('test', 'Deneme');
And lastly, this is my protected/messages/en/test.php file's content:
return array(
'Deneme' => 'Example',
);
Everything OK, it's returning Example . But as you can see, i'm setting language manually on my index action. How can i do it automatically ? Must i add Yii::app()->language='en'; to all actions? How you are using l18n on your projects ?
Note : I'm Yii and l18n noob, so please describe step by step .
Thank you.

You should set the target language in CWebApplication:beginRequest()
in protected/config/main.php, add:
'onBeginRequest' => array('MyApp', 'beginRequest')
In protected/components, create a file MyApp.php, and add this class:
class MyApp {
public static function beginRequest(CEvent $event) {
//set your language, theme, etc here
}
}
Remember to declare beginRequest() as static, or you will encounter errors like this:
https://github.com/yiisoft/yii/issues/794

it's fairly simple. You do all language translations as you said. Then, in the parent controller, in the init method, yo can check the desired language and set the current language. That way, you don't have to do that in every action, just once.

in the Yii's tutorials there is an article that has explained it very good.
in this way you have 3 file: one, your language selector, one, language selector's widget and one is an behavior for handling your language selector file.
read here and use it...
Manage (Target) Language in Multilingual Applications + A Language Selector Widget (i18n)

Related

Set language of TYPO3 StandaloneView in CommandController

I'm using the TYPO3\CMS\Fluid\View\StandaloneView in a CommandController to send emails to my fe_user.
The part where im building the template looks like this:
/* #var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
$emailView = $this->objectManager->get( 'TYPO3\\CMS\\Fluid\\View\\StandaloneView' );
// pass extension name to standaloneView for translations
$extensionName = $this->request->getControllerExtensionName();
$emailView->getRequest()->setControllerExtensionName( $extensionName );
$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath( 'my_extension' );
$templatePathAndFilename = $extensionPath . 'Resources/Private/Templates/Email/' . $templateFile . '.html';
$emailView->setLayoutRootPath( $extensionPath . 'Resources/Private/Layouts/Email/' );
//$emailView->setPartialRootPath($ressourcePath . 'Partials/');
$emailView->setTemplatePathAndFilename( $templatePathAndFilename );
return $emailView->render();
Nothing special i think.
In the html-Template i'm using the normal translate ViewHelper of Fluid:
<f:translate key="LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:mail.text" />
This all works well in the standard language, but i've no idea how i can tell the View/CommandController which language to use.
I'm not quite sure if this is a StandaloneView or a CommandController problem...
Edit:
The posted snippet is called from the CommandController (Scheduler) - so there is no current frontend user (we are in backend environment). I get the user from a repository so i've to save the selected language in there. But then i've to set the language for the StandaloneView
Generally you can set the language in a CommandController by doing this:
$GLOBALS['BE_USER']->uc['lang'] = 'de';
The problem is that the LocalizationUtility creates a cache for the extension after it's been called once to ::translate() something. There is no available function to reset that cache, but you can add this little utility function in your extension to enable you to do just that:
namespace Vendor\Extension\Utility;
class LocalizationUtility extends \TYPO3\CMS\Extbase\Utility\LocalizationUtility
{
public static function resetExtensionLangCache($extensionName){
unset(static::$LOCAL_LANG[$extensionName]);
}
}
If you have the case where you have to switch the full language context in your CommandController you can now call this right after switching the language for the BE_USER:
\Vendor\Extension\Utility\LocalizationUtility::resetExtensionLangCache('<extensionname>');
This will reset the cache and LocalizationUtility will have to initialize its entry for your extension until you switch and call it again.
To control the language used in a Fluid StandaloneView from the backend context, just set the language as shown below (I set the language to german).
$GLOBALS['BE_USER']->uc['lang'] = 'de';
This should be set before you call the $emailView->render() method.
I assume, that you want to send an email in language currently used by user, in that case, you should create language config for the additional languages, like described in TYPO3 docs.
When configured properly and non-default language is used, all your views (also these standalone) will be translated to the current language.
AFAUK, there's no way to force translate VH to use some language ie. by giving it's uid.

I need to direct modify the $sf_content what is the best workaround?

Situation: only main page is accessible by default, all other pages needs a logged in user. When a module is loaded without user, a login template should be displayed, and no module. In other words, the $sf_content must be emptied in layout.php which is not 100% ok since there is logic in the layout. Is there elegant way for that? I dont think a helper is OK either....
Check out security filters, this is one standard way security is designed in symfony.
You even can implement your own SecurityFilter class with the functionality you want.
http://symfony.com/legacy/doc/reference/1_4/en/12-Filters#chapter_12_security
It is done by default for you by the sfBasicSecurityFilter filter. You just need a good configuration. Read this part of the Jobeet tutorial. You should use sfDoctrineGuardPlugin (or sfGuardPlugin if you using propell) for user authentication.
To complete my comments above: There are different ways to override the layout. You could use the methods:
setLayout($name)
//or using foward, which forwards current action to a new one (without browser redirection)
forward($module, $action);
inside your action class. In case you wand to modify the layout inside a filter, you can use something simular to this:
class yourFilter extends sfFilter {
public function execute($filterChain) {
if($yourConditionForOverrideTheDefaultLayout) {
//here the syntax to change the layout from the filer
$actionStack = $this->getContext()->getActionStack();
$actionStack->getFirstEntry()->getActionInstance()->setLayout('yourLayout');
}
$filterChain->execute();
}
}
To avoid unnecessary duplication in the layout file you can work with Fragments and Partials.

i18n not working in Yii user-defined components

I created a simple component in my Yii project and there is a text on it that needs to be translated using i18n feature of Yii
class MySimpleComponent{
public static function WelcomeCurrentUser(){
$wuser = Yii::t("lstrings","Welcome back ");
$wuser .= Yii::app()->session['curuser'];
return $wuser;
}
}
I invoke that small component function in one of my views.
echo MySimpleComponent::WelcomeCurrentUser();
but as i change the language in one of my controllers using the following code
Yii::app()->language = 'ja' //change language to japanese
the translation doesn't work... I double check my i18n strings to ensure that messages are exact to each other and still doesn't work.
this is my lstrings.php file
return array("Hello"=>"こんにちは",
"Welcome back "=>"お帰りなさい",
);
Any ideas why the translation does not work? any solutions? thanks
Call
Yii::app()->language = 'ja'
before
echo MySimpleComponent::WelcomeCurrentUser();
Language translate will take effect as long as before Yii::t()

Codeigniter Change loaded language

Currently i have a language loaded inside MY_Controller which extends CI_Controller. But inside a special page which controller (let's call it ABC controller) extends MY_Controller, I need to override the loaded language with another language. I tried loading another language inside this ABC controller, but unsuccessful. Is there a way to unload the loaded language and load another language?
an easier way is to reset the language data and is_loaded
$this->lang->is_loaded = array();
$this->lang->language = array();
I know it's a bit late to answer this but I think you can change the config item 'language' dynamically based on page requirement.
$this->config->set_item('language', 'chinese');
$this->config->set_item('language', 'english'); // based on the language folder of course holding language files
I had a requirement to send newsletters in users base lang, and this helped me change the language on the fly, hope this might help..
Have you tried just loading the language file you need?
$this->lang->load('filename', 'language');
It should be then accessible just like your default language. I haven't tested this tho, but from my understanding this should be the way to go about it.
Reference: http://codeigniter.com/user_guide/libraries/language.html
REVISED
I ended up digging a bit more for you, and found that you CANNOT load a default language (define it as default in your controller) and then later try to change it to something else.
Follow these steps:
If you need a language OTHER than english (default), set that in your config.
If you want to load ANOTHER language on a controller basis, you need to define that (most commonly in your constructor using something like session array / user selection.
You cannot load 2 languages (1 in the constructor, then another in a different class.. won't work!)
Reference here per forum posts: http://codeigniter.com/forums/viewthread/176223/
I encounter this problem and find a tricky solution.
$this->lang->load('text', 'english');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
//CI will record your lang file is loaded, unset it and then you will able to load another
//unset the lang file to allow the loading of another file
if(isset($this->lang->is_loaded)){
for($i=0; $i<=sizeof($this->lang->is_loaded); $i++){
unset($this->lang->is_loaded[$i]);
}
}
$this->lang->load('text', 'chinese');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
Hope it helps.
If you have any application installed built in codeigniter and you want add a language pack, just follow these steps:
Add language files in folder application/language/arabic
(I added arabic lang in sma2 built in ci)
Go to the file named setting.php
In application/modules/settings/views/setting.php you will find the array:
<div class="controls">
<?php /*
$lang = array (
'english' => 'English',
'arabic' => 'Arabic', // +++ Add this line
'spanish' => 'Español'
Now save and run the application.

Symfony - Override sf_format when calling get_partial

I'm making an AJAX call in my symfony project, so it has an sf_format of 'js'. In the actionSuccess.js.php view, I call get_partial to update the content on the page. By default it looks for the partial in 'js' format since the sf_format is still set as 'js'. Is it possible to override the sf_format so that it uses the regular 'html' partial that I already have (so that I don't have to have two identical partials)?
I have had a similar issue.
I looked through the code, and get_partial doesn't give you any scope to change the format looked for ... guess you could modify the code to make that possible if you needed to.
I instead went for switching the request format - also not ideal in my opinion. But better than editing the symfony files.
To do this in the controller:
$request->setRequestFormat('html');
or in the view
$sf_context->getRequest()->setRequestFormat('html');
In both cases, if you want to set this back afterwards, you can retrieve the existing value using getRequestFormat().
if your looking for a more sustainable solution, you could listen to the view.configure_format and set the sfPHPView extension in your appflication configuration.
// in apps/api/config/apiConfiguration.class.php
public function configure() {
$this->dispatcher->connect('view.configure_format', array($this, 'configure_formats'));
}
public function configure_formats(sfEvent $event) {
// change extension, so our module templates and partials
// for xml do not need the .xml.php extension
$event->getSubject()->setExtension('.php');
}

Categories