Yii2: How to change a system message translation file - php

The problem
Yii is providing its own translations for system messages, e.g. 'File upload failed' is translated to 'Das Hochladen der Datei ist gescheitert.' when the language on my website is changed to German. This would be fine but some of the translations are grammatically incorrect and I would like to change them.
I've found the messages file that yii is using to handle the translation: yiisoft>yii2>messages>de>yii.php but I can't make changes to it as its part of the vendor directory and any changes I'd make would be overwritten during the next yii update.
What I've tried
I've tried following the instructions laid out by the users here: https://forum.yiiframework.com/t/translating-system-messages/29733. Which involves making a copy of yii.php, putting it in a new directory, making the desired translation changes and then pointing coreMessages towards it in the config. I've followed all these steps but it doesn't seem to actually do anything for me.

As it's stated in the Guide:
Yii comes with the default translation messages for validation errors and some other strings. These messages are all in the category yii. Sometimes you want to correct the default framework message translation for your application. In order to do so, configure the i18n application component like the following:
'i18n' => [
'translations' => [
'yii' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '#app/messages'
],
],
],
Now you can place your adjusted translations to #app/messages/<language>/yii.php.
BTW - you mentioned that
[...] translations are grammatically incorrect [...]
It would be great for Yii 2 and its community if you could fix the problem - please fork this file and send a PR.

Related

ZF2 'unable to render template' when shifting module names in the array in app config

Good day.
I've just started learning ZF2, replicated the Album example step-by-step, and then decided to make it a bit more interesting by adding user registration, and then make the two work together in some way, so i added a new 'Auth' module.
So, when i only had one module in the module list (aside from the Application module) in application.config.php, it was all fine, but when i added the Auth module to the list like so:
'modules' => array('Application', 'Album','Auth',)
i got the following error when trying to access any views from the album module which was working absolutely fine prior to this change:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
But when i changed the order in which the modules are presented in this array like so:
'modules' => array('Application', 'Auth','Album',)
not a single view (and it has only one) from the Auth module could be rendered, while the Album module was fine.
Zend\View\Renderer\PhpRenderer::render: Unable to render template "auth/user/index"; resolver could not resolve to a file
Both of these views exist at these locations, but the renderer doesn't see them for some reason.
You can see the project's contents here.
Thank you for any tips in advance.
Looks like you copy pasted the the view manager config for Auth module.config.php.
This should be auth rather than album for your templates to work correctly.
'view_manager' => array(
'template_path_stack' => array(
'auth' => __DIR__ . '/../view',
),
),

Laravel change validation language

How can I change the validator language messages. For example if I want to change the 'required' text 'The kilometerszakelijk field is required.' to something else?
All language specific content is stored in your resources/lang// folder. One of the files you'll find in this folder is validation.php that holds validation messages for given language, so if you want to change the translations, you'll need to edit that file.
Depending on the language that is used by the application, messages will be read from different folders in resources/lang/. You can set language/locale using
App::setLocale('en');
You can also set fallback locale (that is used when there are no translations for current language) by setting it in your config/app.php file, e.g.:
'fallback_locale' => 'en',
See more details in the docs: http://laravel.com/docs/5.0/localization
Create a folder named like your language code in
resources/lang
then create a file in this folder called validation.php and in there, write something like
return [
'required' => 'Das Feld :attribute ist ein Pflichtfeld.',
]
For the default messages, you can adjust the translations in resources/lang/<language>/validation.php.
You can also pass entirely custom messages to the validator.
Laravel-lang
In this repository, you can find the lang files for the framework PHP, Laravel 4&5.
you can see here for use that.

Yii 2 multitranslation

How to enable multitranslation in yii 2 basic framework? I have tried, but it is not working. I get no error, but translation is not showing. Code:
public function actionLang(){
$lang = \Yii::$app->request->get('lang');
if($lang && in_array($lang,['en-US','ar-SA'])){
$cookie = new Cookie();
$cookie->name = '_lang';
$cookie->value = $lang;
$cookie->expire = time() + 60 * 60 * 24 * 180;
\Yii::$app->response->cookies->add($cookie);
}
$this->redirect(['index']);
}
I'm using this function in SiteController.
Internationalization in Yii is not a one-action job. Here's the documentation on how to make your website multilingual:
https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-i18n.md
If docs are unclear, here is a tutorial:
http://code.tutsplus.com/tutorials/programming-with-yii2-localization-with-i18n--cms-23140
If you have gone through all the steps and merely wish to set the current language, you can use:
\Yii::$app->language = 'xxx';
where xxx is a language code in accordance with ISO 639-2.
Here are the mentioned standard's entries:
http://www.loc.gov/standards/iso639-2/php/code_list.php
First of all, from what I have gathered, you are trying to identify a language requested from the current request by doing $lang = \Yii::$app->request->get('lang'); and then set it in the cookie. In my opinion, this should be used as a "helper", meaning, it is useful to know the language preference of the returning client, but you still have to manage languages via URL, i.e. http://yoursite.com/en or http://yoursite.com/de should serve different languages.
Now, there are actually very good plugins out there for multilingual URL management, such as yii2-localeurls , which you can find here. I use it in production in multiple projects and highly recommend it.
To actually manage translations, you have to have a folder in the root of your project (if you are using advance template, you should have it inside frontend/backend/console ) to store the actual block translations, call it messages .
Now, under #app/messages create folders for each non-default language, for example #app/messages/de for German translations.
If you go your config in #app/config/main.php, look for i18n key inside the components array. If you can't find such a key, simply put the following into the components array:
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
]
],
],
Now, you will need to create a translation file inside the relevant directory. In the above configuration, we declared that the default language used is en, that means that all original messages would be in English. Go to #app/messages/de and create a translations file inside that directory. You can call it whatever you like, but for this example, call it site.php.
In this site.php put the following:
return [
'Translate this!' => 'Your relevant translation here in whichever language',
'Translate this also!!!' => 'Stuff...'
];
If all done correctly, when you access your page via http://yousite.com/de, when using Yii::t('site', 'Translate this!') you should be getting 'Your relevant translation here in whichever language' instead.
The beauty of Yii 2 is that it is extremely well documented. Visit the official documentation if you are stuck, it really explains everything quite well.

YII2 application working on local but PAGE NOT FOUND error on live

I have created a simple application in YII2 and it is working fine at my local machine but giving me Not Found (#404) "Page Not Found" error on live server.
Local URL: http://localhost:8080/basicapp/web/index.php?r=adminPanel%2Fstatemaster%2Findex
Live URL: http://XXXX.com/web/index.php?r=adminPanel%2Fstatemaster%2Findex
I am not using pretty URL, and didn't change in web.php. just added module info in the file. Code Snippet:
'modules' => [
'adminpanel' => [
'class' => 'app\admin\adminpanel',
],
'studentPanel' => [
'class' => 'app\Student\dashboard',
],
],
I can provide detial, whatever is required.
If anyone have similar problem, Just change your naming of models and controllers.
In my problem, model and controller names were like StateMasterController etc but it should be StatemasterController, mind the M.
It works for me, may be this can help you.
Thanks

Testing Yii REST application using a second database

I wrote an API using Yii2 and following the REST guide. My API is working and I want to write some tests for it, so I once again followed the guide on how to run tests and got unit tests working. I then looked around Codeception documentation about testing WebServices and got this working too.
My problem is that API calls are not using my test database. I have two databases, one called db and the other testdb. Here is my config.php file in tests/codeception/config/:
return [
'components' => [
'db' => [
'dsn' => 'mysql:host=localhost;port=8889;dbname=testdb;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
],
'mailer' => [
'useFileTransport' => true,
],
'urlManager' => [
'showScriptName' => true,
],
],
];
I wrote a simple test that send a GET request to an endpoint to retrieve data. My test database is empty so I am expecting to receive an empty response, but I get the content of my other database instead.
I then tried to set YII_ENV to test as described in the Environment Constant section here so that I could test against the env variable YII_ENV_TEST and change the db configuration accordingly. I tried to set this variable in the _bootstrap.php file in the tests/codeception/ folder:
defined('YII_ENV') or define('YII_ENV', 'test');
I then logged the value of YII_ENV in the web/index.php file (index-test.php is not called, might be a problem too), and it is undefined.
What am I doing wrong? I tried including the Yii2 module in my api.suite.yml file but requests don't have return code anymore if I do that, it returns N/A. Is there another way to change which database Yii should use?
You can make an test_config.php file and at the end of the config place this
if (file_exists('protected/config/test_config.php'))
{
include 'test_config.php';
}
the file will be included if it exists. And the file test_config.php should contain the overwritten value for the db connection.
Hope this helps!
Keep on coding!
Ares.
Well I found a "solution" by using this other app template: https://github.com/githubjeka/yii2-rest
The file organization fits my needs better and I can easily configure which database to use.

Categories