Laravel Laravel
  • Prologue

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
  • Setup

    • Installation
    • Configuration
    • Homestead
  • The Basics

    • Routing
    • Middleware
    • Controllers
    • Requests
    • Responses
    • Views
  • Architecture Foundations

    • Service Providers
    • Service Container
    • Contracts
    • Facades
    • Request Lifecycle
    • Application Structure
  • Services

    • Authentication
    • Billing
    • Cache
    • Collections
    • Command Bus
    • Core Extension
    • Elixir
    • Encryption
    • Envoy
    • Errors & Logging
    • Events
    • Filesystem / Cloud Storage
    • Hashing
    • Helpers
    • Localization
    • Mail
    • Package Development
    • Pagination
    • Queues
    • Session
    • Templates
    • Unit Testing
    • Validation
  • Database

    • Basic Usage
    • Query Builder
    • Eloquent ORM
    • Schema Builder
    • Migrations & Seeding
    • Redis
  • Artisan CLI

    • Overview
    • Development
Icon

WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 11.x.

Localization

  • Introduction
  • Language Files
  • Basic Usage
  • Pluralization
  • Validation Localization
  • Overriding Package Language Files

Introduction

The Laravel Lang facade provides a convenient way of retrieving strings in various languages, allowing you to easily support multiple languages within your application.

Language Files

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
        /en
            messages.php
        /es
            messages.php

Example Language File

Language files simply return an array of keyed strings. For example:

<?php

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

Changing The Default Language At Runtime

The default language for your application is stored in the config/app.php configuration file. You may change the active language at any time using the App::setLocale method:

App::setLocale('es');

Setting The Fallback Language

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:

'fallback_locale' => 'en',

Basic Usage

Retrieving Lines From A Language File

echo Lang::get('messages.welcome');

The first segment of the string passed to the get method is the name of the language file, and the second is the name of the line that should be retrieved.

Note: If a language line does not exist, the key will be returned by the get method.

You may also use the trans helper function, which is an alias for the Lang::get method.

echo trans('messages.welcome');

Making Replacements In Lines

You may also define place-holders in your language lines:

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

Then, pass a second argument of replacements to the Lang::get method:

echo Lang::get('messages.welcome', ['name' => 'Dayle']);

Determine If A Language File Contains A Line

if (Lang::has('messages.welcome'))
{
    //
}

Pluralization

Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. You may easily manage this in your language files. By using a "pipe" character, you may separate the singular and plural forms of a string:

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

You may then use the Lang::choice method to retrieve the line:

echo Lang::choice('messages.apples', 10);

You may also supply a locale argument to specify the language. For example, if you want to use the Russian (ru) language:

echo Lang::choice('товар|товара|товаров', $count, [], 'ru');

Since the Laravel translator is powered by the Symfony Translation component, you may also create more explicit pluralization rules easily:

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

Validation

For localization for validation errors and messages, take a look at the documentation on Validation.

Overriding Package Language Files

Many packages ship with their own language lines. Instead of hacking the package's core files to tweak these lines, you may override them by placing files in the resources/lang/packages/{locale}/{package} 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/packages/en/hearthfire/messages.php. In this file you would define only the language lines you wish to override. Any language lines you don't override will still be loaded from the package's language files.

last update:2017-05-16 00:44

成为Laravel合作伙伴

Laravel Partners是提供一流Laravel开发和咨询服务的精英商店。我们每个合作伙伴都可以帮助您制定一个精美,结构完善的项目.

我们的伙伴
Laravel
亮点
  • Our Team
  • 发布说明
  • 入门
  • 路由
  • Blade 模板
  • 身份验证
  • 用户授权
  • Artisan 控制台
  • 数据库
  • Eloquent ORM
  • 测试
资源
  • Laravel Bootcamp
  • Laracasts
  • Laravel News
  • Laracon
  • Laracon EU
  • Laracon India
  • Jobs
  • Forums
  • Trademark
  • 版本发布时间
  • 包开发
  • 命令行应用
  • TALL stack全栈开发
  • Blade UI Kit
  • 前端资源构建
伙伴
  • WebReinvent
  • Vehikl
  • Tighten
  • 64 Robots
  • Active Logic
  • Byte 5
  • Curotec
  • Cyber-Duck
  • DevSquad
  • Jump24
  • Kirschbaum
生态系统
  • Cashier
  • Dusk
  • Echo
  • Envoyer
  • Forge
  • Horizon
  • Nova
  • Octane
  • Sail
  • Sanctum
  • Scout
  • Spark
  • Telescope
  • Valet
  • Vapor

Laravel是一个具有表达力,优雅语法的Web应用程序框架。我们认为,发展必须是一种令人愉悦的创造力,才能真正实现。Laravel试图通过减轻大多数Web项目中使用的常见任务来减轻开发的痛苦.

Laravel是Taylor Otwell的商标.
Copyright © 2011-2025 Laravel中文网 LLC.

  • Twitter
  • GitHub
  • Discord
Laravel 全栈开发网 推荐使用阿里云 按Ctrl+D试试