<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Laravel 的本地化功能 - [簡介](#introduction) - [定義翻譯語句](#defining-translation-strings) - [使用短鍵](#using-short-keys) - [使用翻譯語句作為鍵](#using-translation-strings-as-keys) - [提取翻譯語句](#retrieving-translation-strings) - [翻譯語句中的參數替換](#replacing-parameters-in-translation-strings) - [復數](#pluralization) - [重寫擴展包的語言包](#overriding-package-language-files) <a name="introduction"></a> ## 簡介 Laravel 的本地化功能提供方便的方法來獲取多語言的字符串,讓你的網站可以簡單的支持多語言。 語言包存放在 `resources/lang` 目錄下的文件里。在此目錄中應用支持的每一種語言都應該有一個單獨的子目錄: /resources /lang /en messages.php /es messages.php 語言包簡單地返回鍵值對數組,例如: <?php return [ 'welcome' => 'Welcome to our application' ]; ### 切換語言 應用的默認語言保存在 `config/app.php` 配置文件中。當然,你可以根據需求自由的修改當前設置,可以使用 `App` facade 的 `setLocale` 方法動態地更改現有語言: Route::get('welcome/{locale}', function ($locale) { App::setLocale($locale); // }); 你也可以設置 「備用語言」 ,它將會在現有語言沒有指定語句時被使用。就像默認語言那樣,備用語言也可以在 `config/app.php` 配置文件設置: 'fallback_locale' => 'en', #### 指定當前語言 你可以使用 `App` facade 的 `getLocale` 及 `isLocale` 方法指定當前的語言環境或者檢驗當前語言是否是給定的值: $locale = App::getLocale(); if (App::isLocale('en')) { // } <a name="defining-translation-strings"></a> ## 定義翻譯語句 <a name="using-short-keys"></a> ### 使用短鍵 通常,語言包存放在 `resources/lang` 目錄下的文件里。在此目錄中應用支持的每一種語言都應該有一個單獨的子目錄: /resources /lang /en messages.php /es messages.php 語言包簡單地返回鍵值對數組,例如: <?php // resources/lang/en/messages.php return [ 'welcome' => 'Welcome to our application' ]; <a name="using-translation-strings-as-keys"></a> ### 使用翻譯語句作為鍵 對于有大量翻譯需求的應用, 如果每一條翻譯語句都使用 「短鍵」 來定義,那么當你在視圖中嘗試去引用這些 「短鍵」 的時候,很容易變得混亂,分不清哪個對應哪個。因此,Laravel 也提供支持使用 「默認語言」 的翻譯語句作為鍵,來定義其他語言的翻譯語句。 使用翻譯語句作為鍵的語言包需要在 `resources/lang` 目錄下保存為 JSON 文件。例如,如果你的應用中有西班牙語的語言包,你應該新建一個 `resources/lang/es.json` 文件: { "I love programming.": "Me encanta programar." } <a name="retrieving-translation-strings"></a> ## 提取翻譯語句 你可以使用 `__` 輔助函數來獲取翻譯語句,`__` 方法接受文件名和鍵值作為其第一個參數。例如,讓我們提取 `resources/lang/messages.php` 中的 `welcome` : echo __('messages.welcome'); echo __('I love programming.'); 當然,如果你使用 [Blade 模板引擎](/docs/{{version}}/blade), 那么你可以在視圖文件中使用 `{{ }}` 語法或者使用 `@lang` 指令來輸出語句: {{ __('messages.welcome') }} @lang('messages.welcome') 如果指定的語句不存在,`__` 方法則會簡單的返回這個鍵名。所以,如果上述示例中的鍵不存在,那么 `__` 方法則會返回 `messages.welcome` 。 <a name="replacing-parameters-in-translation-strings"></a> ### 翻譯語句中的參數替換 如果需要,你也可以在翻譯語句中定義占位符。所有的占位符都使用的 `:` 開頭。例如,你可以自定義一則歡迎消息的占位符: 'welcome' => 'Welcome, :name', 你可以在 `__` 方法中傳遞一個數組作為第二個參數,它會將數組的值替換到語言內容的占位符中: echo __('messages.welcome', ['name' => 'dayle']); 如果你的占位符中包含了首字母大寫或者全體大寫,翻譯過來的內容也會做相應的處理: 'welcome' => 'Welcome, :NAME', // Welcome, DAYLE 'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle <a name="pluralization"></a> ### 復數 復數是個復雜的問題,不同語言對于復數有不同的規則。使用管道符 `|` ,可以區分單復數字符串格式: 'apples' => 'There is one apple|There are many apples', 你甚至可以創建使用更復雜的復數規則,例如根據數量的范圍不同來指定不同翻譯語句: 'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many', 當你定義完復數語句條件的時候,你可以使用 `trans_choice` 方法來設置「總數」以獲取符合對應條件的復數翻譯語句。例如,在這個例子中,設置「總數」為 10 ,符合數量范圍 1 至 19,所以會得到 `There are some` 這條復數語句: echo trans_choice('messages.apples', 10); <a name="overriding-package-language-files"></a> ## 重寫擴展包的語言包 部分擴展包帶有自己的語言包,你可以通過在 `resources/lang/vendor/{package}/{locale}` 放置文件來重寫它們,而不是直接修改擴展包的核心文件。 例如,你需要重寫 `skyrim/hearthfire` 擴展包的英文語言包 `messages.php` ,則需要把文件放置在 `resources/lang/vendor/hearthfire/en/messages.php` 。在這個文件中定義你想要重寫的翻譯語句,所有沒有重寫的語句將會加載擴展包的語言包中原來的語句。 --- > {note} 歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動共創開源社區。 > > 轉載請注明:本文檔由 Laravel China 社區 [laravel-china.org](https://laravel-china.org) 組織翻譯,詳見 [翻譯召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)。 > > 文檔永久地址: https://d.laravel-china.org
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看