Skip to content

Commit

Permalink
working on validation docs
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 15, 2016
1 parent bbcb328 commit 8ba2bbd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
46 changes: 23 additions & 23 deletions localization.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Localization

- [Introduction](#introduction)
- [Basic Usage](#basic-usage)
- [Retrieving Language Lines](#retrieving-language-lines)
- [Replacing Parameters In Language Lines](#replacing-parameters-in-language-lines)
- [Pluralization](#pluralization)
- [Overriding Vendor Language Files](#overriding-vendor-language-files)
- [Overriding Package Language Files](#overriding-package-language-files)

<a name="introduction"></a>
## Introduction

Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application.

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:
Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application. 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:

/resources
/lang
Expand All @@ -27,7 +26,7 @@ All language files simply return an array of keyed strings. For example:
'welcome' => 'Welcome to our application'
];

#### Configuring The Locale
### Configuring The Locale

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:

Expand All @@ -37,24 +36,24 @@ The default language for your application is stored in the `config/app.php` conf
//
});

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 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:

'fallback_locale' => 'en',

You may check if a given locale is currently being used by calling the `isLocale` method on the `App` [facade](/docs/{{version}}/facades):
#### Determining The Current Locale

You may use the `getLocale` and `isLocale` methods on the `App` facade to determine the current locale or check if the locale is a given value:

$locale = App::getLocale();

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

To retrieve the current application locale, call the `getLocale` method on the `App` [facade](/docs/{{version}}/facades):

return App::getLocale();

<a name="basic-usage"></a>
## Basic Usage
<a name="retrieving-language-lines"></a>
## Retrieving Language Lines

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:
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 `welcome` language line from the `resources/lang/messages.php` language file:

echo trans('messages.welcome');

Expand All @@ -66,7 +65,8 @@ Of course if you are using the [Blade templating engine](/docs/{{version}}/blade

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.

#### Replacing Parameters In Language Lines
<a name="replacing-parameters-in-language-lines"></a>
### Replacing Parameters In Language Lines

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:

Expand All @@ -85,21 +85,21 @@ If your place-holder contains all capital letters, or only has its first letter
<a name="pluralization"></a>
### Pluralization

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:
Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. By using a "pipe" character, you may distinguish singular and plural forms of a string:

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

Then, you may 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:
After defining a language line that has pluralization options, you may 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:

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

Since the Laravel translator is powered by the Symfony Translation component, you may create even more complex pluralization rules:
Since the Laravel translator is powered by the Symfony Translation component, you may create even more complex pluralization rules which specify language lines for multiple number ranges:

'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',

<a name="overriding-vendor-language-files"></a>
## Overriding Vendor Language Files
<a name="overriding-package-language-files"></a>
## Overriding Package Language Files

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.
Some packages may ship with their own language files. Instead of changing the package's core files to tweak these lines, you may override them by placing 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.
So, for example, if you need to override the English language lines in `messages.php` for a package named `skyrim/hearthfire`, you should place a language file at: `resources/lang/vendor/hearthfire/en/messages.php`. Within 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.
38 changes: 19 additions & 19 deletions validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
- [Creating The Controller](#quick-creating-the-controller)
- [Writing The Validation Logic](#quick-writing-the-validation-logic)
- [Displaying The Validation Errors](#quick-displaying-the-validation-errors)
- [Validating Arrays](#validating-arrays)
- [Form Request Validation](#form-request-validation)
- [Creating Form Requests](#creating-form-requests)
- [Authorizing Form Requests](#authorizing-form-requests)
Expand All @@ -20,6 +19,7 @@
- [Custom Error Messages](#custom-error-messages)
- [Available Validation Rules](#available-validation-rules)
- [Conditionally Adding Rules](#conditionally-adding-rules)
- [Validating Arrays](#validating-arrays)
- [Custom Validation Rules](#custom-validation-rules)

<a name="introduction"></a>
Expand Down Expand Up @@ -186,24 +186,6 @@ If you wish to customize the format of the validation errors that are flashed to

In this example, we used a traditional form to send data to the application. However, many applications use AJAX requests. When using the `validate` method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.

<a name="validating-arrays"></a>
### Validating Arrays

Validating array form input fields doesn't have to be a pain. For example, to validate that each e-mail in a given array input field is unique, you may do the following:

$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);

Likewise, you may use the `*` character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:

'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],

<a name="form-request-validation"></a>
## Form Request Validation

Expand Down Expand Up @@ -913,6 +895,24 @@ The first argument passed to the `sometimes` method is the name of the field we

> {tip} The `$input` parameter passed to your `Closure` will be an instance of `Illuminate\Support\Fluent` and may be used to access your input and files.
<a name="validating-arrays"></a>
## Validating Arrays

Validating array based form input fields doesn't have to be a pain. For example, to validate that each e-mail in a given array input field is unique, you may do the following:

$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);

Likewise, you may use the `*` character when specifying your validation messages in your language files, making it a breeze to use a single validation message for array based fields:

'custom' => [
'person.*.email' => [
'unique' => 'Each person must have a unique e-mail address',
]
],

<a name="custom-validation-rules"></a>
## Custom Validation Rules

Expand Down

0 comments on commit 8ba2bbd

Please sign in to comment.