Ray Lee | 李宗叡
Learn or Die
Published in
7 min readMar 21, 2021

--

Photo by Sigmund on Unsplash

# 版本

Laravel 8.x

# 前言

我喜歡使用 Laravel 開發的感覺, 除了開發快速, 程式碼簡潔且優雅之外, Laravel 框架本身也是一個很好的學習參照物。 本篇主要將官方文件重點整理成 Q&A 的形式呈現, 原子化的概念, 這方式並不適用於每個人, 但若對你有幫助, 我會很開心。

# 目錄

Laravel — 官方文件原子化翻譯 — 目錄

以下的 Laravel example directory structure 的意思是?

  • Example1:
/resources
/lang
/en
messages.php
/es
messages.php
  • Example2:
/resources
/lang
en.json
es.json
  • Answer: 為 localization 的翻譯檔案置放規則 1 為 Laravel 預設的方式, 2 適合當有大量的翻譯檔案時

以下的 Laravel example code 的意思是?

  • Example:
<?php
use Illuminate\Support\Facades\App;

Route::get('/greeting/{locale}', function ($locale) {
if (! in_array($locale, ['en', 'es', 'fr'])) {
abort(400);
}

App::setLocale($locale);

//
});
  • Answer: 使用 API 來調整 locale, 如果沒有指定的語言則報錯

以下的 Laravel example code 的意思是?

  • Example:
<?php
'fallback_locale' => 'en',
  • Answer: 當 default locale file 沒有相對應的 transaction string 時, 使用 fallback locale

# Determining The Current Locale

以下的 Laravel example code 的意思是?

  • Example:
<?php
use Illuminate\Support\Facades\App;

$locale = App::currentLocale();

if (App::isLocale('en')) {
//
}
  • Answer: 取得 current locale 以及判斷 current locale

# Defining Translation Strings

# Using Short Keys

以下的 Laravel example locale code 的意思是?

  • Example:
<?php
{
"I love programming.": "Me encanta programar."
}
  • Answer: use translation string as key

以下的 Laravel example code 中, 如果 nl.json 檔案中沒有 ‘Action’ string, 但有 nl/action 這個檔案, 那會使用哪一個?

  • Example:
<?php
__('Action')
  • Answer: 會使用 nl/action 這個檔案, 所以務必要避免衝突

# Retrieving Translation Strings

以下的 Laravel example code 的意思是?

  • Example:
<?php
echo __('messages.welcome');
  • Answer: 取得 default locale directory 中的 messages file 中的 welcome string key 的 value

以下的 Laravel example code 的意思是?

  • Example:
<?php
echo __('I love programming.');
  • Answer: 取得 default locale json file 中的 translation string key 的 value

以下的 Laravel example code 的意思是?

  • Example:
<?php
{{ __('messages.welcome') }}
  • Answer: 在 blade method 中使用 __() 取得 default locale directory 中, message file 中的 welcome string key 的 value

# Replacing Parameters In Translation Strings

以下的 Laravel example code 的意思是?

  • Example:
<?php
// 在 localization 檔案中
'welcome' => 'Welcome, :NAME',
'goodbye' => 'Goodbye, :Name',

// controller 內
echo __('messages.welcome', ['name' => 'dayle']);
  • Answer: // Welcome, DAYLE // Goodbye, Dayle 可在 localization 檔案中定義 placeholder, 並如果 placeholder 是大寫開頭, 帶進去的值也會自動地被 capitalised

# Pluralization

以下的 Laravel example code 的意思是?

  • Example:
<?php
// in localization file
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',

// in controller
echo trans_choice('time.minutes_ago', 5, ['value' => 5]);
  • Answer: Localization file 內, 可使用 | 定義 singular 以及 plural transaction string, 然後透過 trans_choice() arg2 帶入的參數來決定 singular 或 plural, 也可在 arg3 帶入 placeholder 的 value

以下的 Laravel example code 的意思是?

  • Example:
<?php
{
"There is one apple|There are many apples": "Hay una manzana|Hay muchas manzanas"
}

// 或
'apples' => 'There is one apple|There are many apples',

// in controller
trans_choice('message.apples', 5)
  • Answer: // There are many apples 使用 | 來定義單複數, trans_choice arg2 可判斷單複數, 並取得定義好的 string

以下的 Laravel example code 的意思是?

  • Example:
<?php
// in localization file
'apples' => '{0} There are none|{1} There is one|[2,*] There are :count',

// in controller
return trans_choice('message.apples', 10)
  • Answer: // There are 10 若要 display arg2 integer value, 可使用 default 的 :count

# Overriding Package Language Files

Laravel 中, 假如有個 package 有自己的 localization file, 該 package 名為 skyrim/hearthfire, 那我該怎麼覆寫它?

resources/lang/vendor/hearthfire/en/messages.php

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.