localization.md 2.94 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7 8 9 10 11 12 13
# Localization

## Contents

- [The Basics](#the-basics)
- [Retrieving A Language Line](#get)
- [Place Holders & Replacements](#replace)

<a name="the-basics"></a>
## The Basics

Localization is the process of translating your application into different languages. The **Lang** class provides a simple mechanism to help you organize and retrieve the text of your multilingual application.

14
All of the language files for your application live under the **application/language** directory. Within the **application/language** directory, you should create a directory for each language your application speaks. So, for example, if your application speaks English and Spanish, you might create **en** and **es** directories under the **language** directory.
Taylor Otwell committed
15 16 17 18 19 20 21 22 23 24 25

Each language directory may contain many different language files. Each language file is simply an array of string values in that language. In fact, language files are structured identically to configuration files. For example, within the **application/language/en** directory, you could create a **marketing.php** file that looks like this:

#### Creating a language file:

	return array(

	     'welcome' => 'Welcome to our website!',

	);

26
Next, you should create a corresponding **marketing.php** file within the **application/language/es** directory. The file would look something like this:
Taylor Otwell committed
27 28 29 30 31 32 33 34 35

	return array(

	     'welcome' => 'Bienvenido a nuestro sitio web!',

	);

Nice! Now you know how to get started setting up your language files and directories. Let's keep localizing!

36
<a name="get"></a>
Taylor Otwell committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
## Retrieving A Language Line

#### Retrieving a language line:

	echo Lang::line('marketing.welcome')->get();

#### Retrieving a language line using the "__" helper:

	echo __('marketing.welcome');

Notice how a dot was used to separate "marketing" and "welcome"? The text before the dot corresponds to the language file, while the text after the dot corresponds to a specific string within that file.

Need to retrieve the line in a language other than your default? Not a problem. Just mention the language to the **get** method:

#### Getting a language line in a given language:

53
	echo Lang::line('marketing.welcome')->get('es');
Taylor Otwell committed
54 55 56 57

<a name="replace"></a>
## Place Holders & Replacements

Pascal Borreli committed
58
Now, let's work on our welcome message. "Welcome to our website!" is a pretty generic message. It would be helpful to be able to specify the name of the person we are welcoming. But, creating a language line for each user of our application would be time-consuming and ridiculous. Thankfully, you don't have to. You can specify "place-holders" within your language lines. Place-holders are preceded by a colon:
Taylor Otwell committed
59 60 61 62 63 64 65 66 67 68 69 70

#### Creating a language line with place-holders:

	'welcome' => 'Welcome to our website, :name!'

#### Retrieving a language line with replacements:

	echo Lang::line('marketing.welcome', array('name' => 'Taylor'))->get();

#### Retrieving a language line with replacements using "__":

	echo __('marketing.welcome', array('name' => 'Taylor'));