Skip to content

Commit 86834ea

Browse files
committed
Added i18n support for my Yii2 web app
1 parent 503912b commit 86834ea

File tree

8 files changed

+430
-0
lines changed

8 files changed

+430
-0
lines changed

config/i18n.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
return [
4+
// string, required, root directory of all source files
5+
'sourcePath' => __DIR__ . DIRECTORY_SEPARATOR . '..',
6+
// array, required, list of language codes that the extracted messages
7+
// should be translated to. For example, ['zh-CN', 'de'].
8+
'languages' => ['en', 'es'],
9+
// string, the name of the function for translating messages.
10+
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
11+
// translated. You may use a string for single function name or an array for
12+
// multiple function names.
13+
'translator' => 'Yii::t',
14+
// boolean, whether to sort messages by keys when merging new messages
15+
// with the existing ones. Defaults to false, which means the new (untranslated)
16+
// messages will be separated from the old (translated) ones.
17+
'sort' => false,
18+
// boolean, whether to remove messages that no longer appear in the source code.
19+
// Defaults to false, which means each of these messages will be enclosed with a pair of '@@' marks.
20+
'removeUnused' => false,
21+
// array, list of patterns that specify which files (not directories) should be processed.
22+
// If empty or not set, all files will be processed.
23+
// Please refer to "except" for details about the patterns.
24+
'only' => ['*.php'],
25+
// array, list of patterns that specify which files/directories should NOT be processed.
26+
// If empty or not set, all files/directories will be processed.
27+
// A path matches a pattern if it contains the pattern string at its end. For example,
28+
// '/a/b' will match all files and directories ending with '/a/b';
29+
// the '*.svn' will match all files and directories whose name ends with '.svn'.
30+
// and the '.svn' will match all files and directories named exactly '.svn'.
31+
// Note, the '/' characters in a pattern matches both '/' and '\'.
32+
// See helpers/FileHelper::findFiles() description for more details on pattern matching rules.
33+
// If a file/directory matches both a pattern in "only" and "except", it will NOT be processed.
34+
'except' => [
35+
'.svn',
36+
'.git',
37+
'.gitignore',
38+
'.gitkeep',
39+
'.hgignore',
40+
'.hgkeep',
41+
'/messages',
42+
],
43+
44+
// 'php' output format is for saving messages to php files.
45+
'format' => 'php',
46+
// Root directory containing message translations.
47+
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . 'messages',
48+
// boolean, whether the message file should be overwritten with the merged messages
49+
'overwrite' => true,
50+
51+
/*
52+
// Message categories to ignore
53+
'ignoreCategories' => [
54+
'yii',
55+
],
56+
*/
57+
58+
/*
59+
// 'db' output format is for saving messages to database.
60+
'format' => 'db',
61+
// Connection component to use. Optional.
62+
'db' => 'db',
63+
// Custom source message table. Optional.
64+
// 'sourceMessageTable' => '{{%source_message}}',
65+
// Custom name for translation message table. Optional.
66+
// 'messageTable' => '{{%message}}',
67+
*/
68+
69+
/*
70+
// 'po' output format is for saving messages to gettext po files.
71+
'format' => 'po',
72+
// Root directory containing message translations.
73+
'messagePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages',
74+
// Name of the file that will be used for translations.
75+
'catalog' => 'messages',
76+
// boolean, whether the message file should be overwritten with the merged messages
77+
'overwrite' => true,
78+
*/
79+
];

config/web.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
'id' => 'basic',
77
'basePath' => dirname(__DIR__),
88
'bootstrap' => ['log'],
9+
// default language spanish
10+
'language' => 'es',
911
'components' => [
1012
'request' => [
1113
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation

controllers/LangController.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace app\controllers;
4+
5+
use \Yii;
6+
use \yii\web\Controller;
7+
8+
/**
9+
* Language Controller
10+
*/
11+
class LangController extends Controller
12+
{
13+
public function actionIndex()
14+
{
15+
return $this->render('index');
16+
}
17+
18+
public function actionChangeLang($lang)
19+
{
20+
$session = \Yii::$app->session;
21+
$session->set('lang', $lang);
22+
\Yii::$app->language = \Yii::$app->session['lang'];
23+
return $this->render('index');
24+
}
25+
}

messages/en/app.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Aquí {cats, plural, =0{no hay gatos} =1{hay un gato} other{hay # gatos}}!' => 'There {cats, plural, =0{are not cats} =1{is a cat} other{are # cats}}!',
21+
'Hola, {username}!' => 'Hi, {username}!',
22+
'Hoy: {0, date, dd-M-yyyy}' => 'Today: {0, date, yyyy-M-dd}',
23+
'Precio: {0, number, integer}' => 'Price: {0, number, integer}',
24+
'Precio: {0}' => 'Price: {0}',
25+
'Precio: {0}, Conteo: {1}, Subtotal: {2}' => 'Price: {0}, Count: {1}, Subtotal: {2}',
26+
'Tu edad es {age}' => 'Your age is {age}',
27+
];

messages/en/yii.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'(not set)' => '',
21+
'An internal server error occurred.' => '',
22+
'Are you sure you want to delete this item?' => '',
23+
'Delete' => '',
24+
'Error' => '',
25+
'File upload failed.' => '',
26+
'Home' => '',
27+
'Invalid data received for parameter "{param}".' => '',
28+
'Login Required' => '',
29+
'Missing required arguments: {params}' => '',
30+
'Missing required parameters: {params}' => '',
31+
'No' => '',
32+
'No results found.' => '',
33+
'Only files with these MIME types are allowed: {mimeTypes}.' => '',
34+
'Only files with these extensions are allowed: {extensions}.' => '',
35+
'Page not found.' => '',
36+
'Please fix the following errors:' => '',
37+
'Please upload a file.' => '',
38+
'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.' => '',
39+
'The file "{file}" is not an image.' => '',
40+
'The file "{file}" is too big. Its size cannot exceed {limit, number} {limit, plural, one{byte} other{bytes}}.' => '',
41+
'The file "{file}" is too small. Its size cannot be smaller than {limit, number} {limit, plural, one{byte} other{bytes}}.' => '',
42+
'The format of {attribute} is invalid.' => '',
43+
'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '',
44+
'The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '',
45+
'The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '',
46+
'The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.' => '',
47+
'The requested view "{name}" was not found.' => '',
48+
'The verification code is incorrect.' => '',
49+
'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.' => '',
50+
'Unable to verify your data submission.' => '',
51+
'Unknown option: --{name}' => '',
52+
'Update' => '',
53+
'View' => '',
54+
'Yes' => '',
55+
'You are not allowed to perform this action.' => '',
56+
'You can upload at most {limit, number} {limit, plural, one{file} other{files}}.' => '',
57+
'in {delta, plural, =1{a day} other{# days}}' => '',
58+
'in {delta, plural, =1{a minute} other{# minutes}}' => '',
59+
'in {delta, plural, =1{a month} other{# months}}' => '',
60+
'in {delta, plural, =1{a second} other{# seconds}}' => '',
61+
'in {delta, plural, =1{a year} other{# years}}' => '',
62+
'in {delta, plural, =1{an hour} other{# hours}}' => '',
63+
'just now' => '',
64+
'the input value' => '',
65+
'{attribute} "{value}" has already been taken.' => '',
66+
'{attribute} cannot be blank.' => '',
67+
'{attribute} is invalid.' => '',
68+
'{attribute} is not a valid URL.' => '',
69+
'{attribute} is not a valid email address.' => '',
70+
'{attribute} must be "{requiredValue}".' => '',
71+
'{attribute} must be a number.' => '',
72+
'{attribute} must be a string.' => '',
73+
'{attribute} must be an integer.' => '',
74+
'{attribute} must be either "{true}" or "{false}".' => '',
75+
'{attribute} must be greater than "{compareValue}".' => '',
76+
'{attribute} must be greater than or equal to "{compareValue}".' => '',
77+
'{attribute} must be less than "{compareValue}".' => '',
78+
'{attribute} must be less than or equal to "{compareValue}".' => '',
79+
'{attribute} must be no greater than {max}.' => '',
80+
'{attribute} must be no less than {min}.' => '',
81+
'{attribute} must be repeated exactly.' => '',
82+
'{attribute} must not be equal to "{compareValue}".' => '',
83+
'{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.' => '',
84+
'{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.' => '',
85+
'{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.' => '',
86+
'{delta, plural, =1{a day} other{# days}} ago' => '',
87+
'{delta, plural, =1{a minute} other{# minutes}} ago' => '',
88+
'{delta, plural, =1{a month} other{# months}} ago' => '',
89+
'{delta, plural, =1{a second} other{# seconds}} ago' => '',
90+
'{delta, plural, =1{a year} other{# years}} ago' => '',
91+
'{delta, plural, =1{an hour} other{# hours}} ago' => '',
92+
'{nFormatted} B' => '',
93+
'{nFormatted} GB' => '',
94+
'{nFormatted} GiB' => '',
95+
'{nFormatted} KB' => '',
96+
'{nFormatted} KiB' => '',
97+
'{nFormatted} MB' => '',
98+
'{nFormatted} MiB' => '',
99+
'{nFormatted} PB' => '',
100+
'{nFormatted} PiB' => '',
101+
'{nFormatted} TB' => '',
102+
'{nFormatted} TiB' => '',
103+
'{nFormatted} {n, plural, =1{byte} other{bytes}}' => '',
104+
'{nFormatted} {n, plural, =1{gibibyte} other{gibibytes}}' => '',
105+
'{nFormatted} {n, plural, =1{gigabyte} other{gigabytes}}' => '',
106+
'{nFormatted} {n, plural, =1{kibibyte} other{kibibytes}}' => '',
107+
'{nFormatted} {n, plural, =1{kilobyte} other{kilobytes}}' => '',
108+
'{nFormatted} {n, plural, =1{mebibyte} other{mebibytes}}' => '',
109+
'{nFormatted} {n, plural, =1{megabyte} other{megabytes}}' => '',
110+
'{nFormatted} {n, plural, =1{pebibyte} other{pebibytes}}' => '',
111+
'{nFormatted} {n, plural, =1{petabyte} other{petabytes}}' => '',
112+
'{nFormatted} {n, plural, =1{tebibyte} other{tebibytes}}' => '',
113+
'{nFormatted} {n, plural, =1{terabyte} other{terabytes}}' => '',
114+
'{percent}% Complete' => '',
115+
];

messages/es/app.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Message translations.
4+
*
5+
* This file is automatically generated by 'yii message' command.
6+
* It contains the localizable messages extracted from source code.
7+
* You may modify this file by translating the extracted messages.
8+
*
9+
* Each array element represents the translation (value) of a message (key).
10+
* If the value is empty, the message is considered as not translated.
11+
* Messages that no longer need translation will have their translations
12+
* enclosed between a pair of '@@' marks.
13+
*
14+
* Message string can be used with plural forms format. Check i18n section
15+
* of the guide for details.
16+
*
17+
* NOTE: this file must be saved in UTF-8 encoding.
18+
*/
19+
return [
20+
'Aquí {cats, plural, =0{no hay gatos} =1{hay un gato} other{hay # gatos}}!' => '',
21+
'Hola, {username}!' => '',
22+
'Hoy: {0, date, dd-M-yyyy}' => '',
23+
'Precio: {0, number, integer}' => '',
24+
'Precio: {0}' => '',
25+
'Precio: {0}, Conteo: {1}, Subtotal: {2}' => '',
26+
'Tu edad es {age}' => '',
27+
];

0 commit comments

Comments
 (0)