You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Overriding Vendor Language Files](#overriding-vendor-language-files)
3
+
-[介绍](#introduction)
4
+
-[基本用法](#basic-usage)
5
+
-[复数形式](#pluralization)
6
+
-[覆写扩展包的语言文件](#overriding-vendor-language-files)
7
7
8
8
<aname="introduction"></a>
9
-
## Introduction
9
+
## 介绍
10
10
11
-
Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.
11
+
Laravel的本地化功能提供了一种便利的获取多种语言字串的方法,让你轻松地在应用中支持多语言。
12
12
13
-
Language strings are stored in files within the `resources/lang`directory. Within this directory there should be a subdirectory for each language supported by the application:
@@ -19,69 +19,69 @@ Language strings are stored in files within the `resources/lang` directory. With
19
19
/es
20
20
messages.php
21
21
22
-
All language files simply return an array of keyed strings. For example:
22
+
所有语言文件都简单返回一个带有键值的数组。例如:
23
23
24
24
<?php
25
25
26
26
return [
27
27
'welcome' => 'Welcome to our application'
28
28
];
29
29
30
-
#### Configuring The Locale
30
+
#### 配置语言环境
31
31
32
-
The default language for your application is stored in the `config/app.php`configuration file. Of course, you may modify this value to suit the needs of your application. You may also change the active language at runtime using the `setLocale` method on the `App` facade:
Route::get('welcome/{locale}', function ($locale) {
35
35
App::setLocale($locale);
36
36
37
37
//
38
38
});
39
39
40
-
You may also configure a "fallback language", which will be used when the active language does not contain a given language line. Like the default language, the fallback language is also configured in the `config/app.php`configuration file:
You may retrieve lines from language files using the `trans`helper function. The `trans`method accepts the file and key of the language line as its first argument. For example, let's retrieve the language line `welcome` in the `resources/lang/messages.php`language file:
If the specified language line does not exist, the `trans`function will simply return the language line key. So, using the example above, the `trans`function would return `messages.welcome` if the language line does not exist.
If you wish, you may define place-holders in your language lines. All place-holders are prefixed with a `:`. For example, you may define a welcome message with a place-holder name:
59
+
你还可以给语言文件中的各行定义占位符。所有的占位符都以 `:` 为前缀。比如你可以定义一个带有占位符 name 的欢迎信息:
60
60
61
61
'welcome' => 'Welcome, :name',
62
62
63
-
To replace the place-holders when retrieving a language line, pass an array of replacements as the second argument to the `trans`function:
Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. By using a "pipe" character, you may distinguish a singular and plural form of a string:
'apples' => 'There is one apple|There are many apples',
73
73
74
-
Then, you may then use the `trans_choice`function to retrieve the line for a given "count". In this example, since the count is greater than one, the plural form of the language line is returned:
Since the Laravel translator is powered by the Symfony Translation component, you may create even more complex pluralization rules:
78
+
因为 Laravel 的翻译器由 Symfony 翻译组件提供,你可以创建更加复杂的复数规则:
79
79
80
80
'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',
81
81
82
82
<aname="overriding-vendor-language-files"></a>
83
-
## Overriding Vendor Language Files
83
+
## 覆写扩展包的语言文件
84
84
85
-
Some packages may ship with their own language files. Instead of hacking the package's core files to tweak these lines, you may override them by placing your own files in the `resources/lang/vendor/{package}/{locale}`directory.
So, for example, if you need to override the English language lines in `messages.php`for a package named `skyrim/hearthfire`, you would place a language file at: `resources/lang/vendor/hearthfire/en/messages.php`. In this file you should only define the language lines you wish to override. Any language lines you don't override will still be loaded from the package's original language files.
0 commit comments