Laravel 文件閱讀:國際化

zhangbao發表於2017-09-07

翻譯、衍生自:https://learnku.com/docs/laravel/5.5/localization

簡介

Laravel 對國際化的支援非常友好,允許專案支援多種語言。當你看到它的資料夾結構就非常清晰了。

/resources
    /lang
        /en
            messages.php
        /zh-CN
            messages.php

Laravel 的國際化檔案都放在 resources/lang 下面,每種語言對應其中的一個子資料夾,en 指英文國際化檔案,zh-CN 指中文簡體的國際化檔案,你可以按照實際需要建立新的語言資料夾和裡面的國際化檔案。

所有的國際化檔案都是返回一個陣列,陣列的鍵是字串型別的:

<?php

return [
    'welcome' => 'Welcome to our application'
];

配置語言環境

專案使用的預設語言是在 config/app.php 配置檔案裡設定的,你可以按照實際需要修改它。

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'zh-CN',

也可以使用 App 門面的 setLocale 方法靈活改變語言環境。

Route::get('welcome/{locale}', function ($locale) {
    App::setLocale($locale);

    //
});

「回退語言」也是支援的,所謂的回退語言是指當預設語言的國際化文字沒有提供時,就會使用回退語言的對應國際化文字,這個也是在 config/app.php 中配置的:

    /*
    |--------------------------------------------------------------------------
    | Application Fallback Locale
    |--------------------------------------------------------------------------
    |
    | The fallback locale determines the locale to use when the current one
    | is not available. You may change the value to correspond to any of
    | the language folders that are provided through your application.
    |
    */

    'fallback_locale' => 'en',

判斷當前的語言環境

可以使用 App 門面的 getLocale 方法獲得當前的語言環境,使用 isLocale 方法檢驗當前的語言環境是否是給定的值:

$locale = App::getLocale();

if (App::isLocale('en')) {
    //
}

定義翻譯字串

使用簡短的鍵

已經說過,國際化文字儲存在 resources/lang 目錄下。此目錄下的每個子目錄對應專案中支援的一種語言。

/resources
    /lang
        /en
            messages.php
        /zh-CN
            messages.php

所有的語言檔案都簡單的返回一個陣列,陣列的鍵是字串型別的:

<?php

// resources/lang/en/messages.php

return [
    'welcome' => 'Welcome to our application'
];

使用翻譯字串作為鍵

如果一個專案中有大量需要國際化的頁面欄位,這時定義前面這種「簡短的鍵」,就會面臨問題——有時很難分清長相相似的鍵的區別和在哪裡使用了它們。所以,有時使用翻譯字串作為鍵不失為一種好方法。

使用翻譯字串作為鍵的翻譯檔案是以 JSON 格式儲存在 resources/lang 中的。例如,如果專案支援西班牙文。

// config/app.php

'locale' => 'es',

那麼對應的,就要建立一個 resources/lang/es.json 檔案:

{
    "I love programming.": "Me encanta programar."
}

獲得翻譯字串

使用全域性輔助函式 __ 來獲得翻譯字串, __ 函式的第一個引數使用 (指使用翻譯字串作為鍵的鍵) 或者是 檔案.鍵 的形式。

echo __('messages.welcome');

echo __('I love programming.');

如果使用 Blade 模板引擎列印國際化文字,可以使用 {{ }} 語法或者使用 @lang 指令:

{{ __('messages.welcome') }}

@lang('messages.welcome')

如果沒有找到對應的翻譯字串,__ 函式就會返回翻譯字串鍵本身。對於上面的例子,如果翻譯字串不存在,__ 函式就會返回「messages.welcome」這個內容。

在翻譯字串中定義佔位符

也可以在翻譯字串中定義佔位符,所有的佔位符使用 : 作為字首。例如,把使用者名稱作為佔位符:

'welcome' => 'Welcome, :name',

替換佔位符使用 __ 函式的第二個引數:

 echo __('messages.welcome', ['name' => 'dayle']);

如果佔位符全部是大寫字母,或者是首字母大寫。那麼翻譯過來的字串也會是相應的大寫形式:

'welcome' => 'Welcome, :NAME', // Welcome, DAYLE
'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle

複數

複數形式是個老大難問題,因為不同語言的複數規則是不同的。使用「管道」字元,可以用來區分字串的單數和複數形式:

'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 函式獲得給定「數量」的字串文字。在下面的例子中,因為數量大於 1,所以就會返回翻譯字串的複數形式:

echo trans_choice('messages.apples', 10);

覆蓋包語言檔案

一些包自帶語言檔案,你不用更改軟體包的核心檔案來調整預設的翻譯,直接建立 resources/lang/vendor/{package}/{locale} 資料夾就可以覆蓋它們。

例如,對於包 skyrim/hearthfire,如果要覆蓋包中 messages.php 檔案裡的英文翻譯,就應該建立一個檔案 resources/lang/vendor/hearthfire/en/messages.php。在此檔案中,你應該只定義要覆蓋的內容,沒有定義的還是使用原來包語言檔案裡的翻譯內容。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章