CodeIgniter4/outgoing/localization.html
2023-12-28 09:34:43 +00:00

764 lines
56 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Localization &mdash; CodeIgniter 4.4.4 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/citheme_dark.css" type="text/css" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/citheme.js"></script>
<script src="../_static/js/carbon.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Alternate PHP Syntax for View Files" href="alternative_php.html" />
<link rel="prev" title="Content Security Policy" href="csp.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html">
<img src="../_static/ci-logo-text.svg" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static Pages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News Section</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create News Items</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working with HTTP Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Building Responses</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="views.html">Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="view_cells.html">View Cells</a></li>
<li class="toctree-l2"><a class="reference internal" href="view_renderer.html">View Renderer</a></li>
<li class="toctree-l2"><a class="reference internal" href="view_layouts.html">View Layouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="view_parser.html">View Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="view_decorators.html">View Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="csp.html">Content Security Policy</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Localization</a></li>
<li class="toctree-l2"><a class="reference internal" href="alternative_php.html">Alternate PHP Syntax for View Files</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working with Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/file_collections.html">File Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/publisher.html">Publisher</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/mocking.html">Mocking</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_overview.html">CLI Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_controllers.html">Running Controllers via CLI</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/spark_commands.html">Spark Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Creating Spark Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/composer_packages.html">Creating Composer Packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/official_packages.html">Official Packages</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/outgoing/localization.rst">Edit this page</a>
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="index.html">Building Responses</a></li>
<li class="breadcrumb-item active">Localization</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="localization">
<h1>Localization<a class="headerlink" href="#localization" title="Permalink to this headline"></a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#working-with-locales" id="id2">Working with Locales</a></p>
<ul>
<li><p><a class="reference internal" href="#configuring-the-locale" id="id3">Configuring the Locale</a></p>
<ul>
<li><p><a class="reference internal" href="#setting-the-default-locale" id="id4">Setting the Default Locale</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#locale-detection" id="id5">Locale Detection</a></p>
<ul>
<li><p><a class="reference internal" href="#content-negotiation" id="id6">Content Negotiation</a></p></li>
<li><p><a class="reference internal" href="#in-routes" id="id7">In Routes</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#setting-the-current-locale" id="id8">Setting the Current Locale</a></p></li>
<li><p><a class="reference internal" href="#retrieving-the-current-locale" id="id9">Retrieving the Current Locale</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#language-localization" id="id10">Language Localization</a></p>
<ul>
<li><p><a class="reference internal" href="#creating-language-files" id="id11">Creating Language Files</a></p></li>
<li><p><a class="reference internal" href="#basic-usage" id="id12">Basic Usage</a></p>
<ul>
<li><p><a class="reference internal" href="#replacing-parameters" id="id13">Replacing Parameters</a></p></li>
<li><p><a class="reference internal" href="#specifying-locale" id="id14">Specifying Locale</a></p></li>
<li><p><a class="reference internal" href="#nested-arrays" id="id15">Nested Arrays</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#language-fallback" id="id16">Language Fallback</a></p></li>
<li><p><a class="reference internal" href="#message-translations" id="id17">Message Translations</a></p></li>
</ul>
</li>
</ul>
</div>
<section id="working-with-locales">
<h2><a class="toc-backref" href="#id2">Working with Locales</a><a class="headerlink" href="#working-with-locales" title="Permalink to this headline"></a></h2>
<p>CodeIgniter provides several tools to help you localize your application for different languages. While full
localization of an application is a complex subject, its simple to swap out strings in your application
with different supported languages.</p>
<p>Language strings are stored in the <strong>app/Language</strong> directory, with a sub-directory for each
supported language:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">app</span><span class="o">/</span>
<span class="nx">Language</span><span class="o">/</span>
<span class="nx">en</span><span class="o">/</span>
<span class="nx">App</span><span class="o">.</span><span class="nx">php</span>
<span class="nx">fr</span><span class="o">/</span>
<span class="nx">App</span><span class="o">.</span><span class="nx">php</span>
</pre></div>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Locale detection only works for web-based requests that use the IncomingRequest class.
Command-line requests will not have these features.</p>
</div>
<section id="configuring-the-locale">
<h3><a class="toc-backref" href="#id3">Configuring the Locale</a><a class="headerlink" href="#configuring-the-locale" title="Permalink to this headline"></a></h3>
<section id="setting-the-default-locale">
<span id="id1"></span><h4><a class="toc-backref" href="#id4">Setting the Default Locale</a><a class="headerlink" href="#setting-the-default-locale" title="Permalink to this headline"></a></h4>
<p>Every site will have a default language/locale they operate in. This can be set in <strong>app/Config/App.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">App</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="k">public</span> <span class="nx">string</span> <span class="nv">$defaultLocale</span> <span class="o">=</span> <span class="s1">&#39;en&#39;</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The value can be any string that your application uses to manage text strings and other formats. It is
recommended that a <a class="reference external" href="http://www.rfc-editor.org/rfc/bcp/bcp47.txt">BCP 47</a> language code is used. This results in
language codes like en-US for American English, or fr-FR, for French/France. A more readable introduction
to this can be found on the <a class="reference external" href="https://www.w3.org/International/articles/language-tags/">W3Cs site</a>.</p>
<p>The system is smart enough to fall back to more generic language codes if an exact match
cannot be found. If the locale code was set to <code class="docutils literal notranslate"><span class="pre">en-US</span></code> and we only have language files set up for <code class="docutils literal notranslate"><span class="pre">en</span></code>
then those will be used since nothing exists for the more specific <code class="docutils literal notranslate"><span class="pre">en-US</span></code>. If, however, a language
directory existed at the <strong>app/Language/en-US</strong> directory then that would be used first.</p>
</section>
</section>
<section id="locale-detection">
<h3><a class="toc-backref" href="#id5">Locale Detection</a><a class="headerlink" href="#locale-detection" title="Permalink to this headline"></a></h3>
<p>There are two methods supported to detect the correct locale during the request. The first is a “set and forget”
method that will automatically perform <a class="reference internal" href="../incoming/content_negotiation.html"><span class="doc">content negotiation</span></a> for you to
determine the correct locale to use. The second method allows you to specify a segment in your routes that
will be used to set the locale.</p>
<p>Should you ever need to set the locale directly, see <a class="reference internal" href="#setting-the-current-locale">Setting the Current Locale</a>.</p>
<p>Since v4.4.0, <code class="docutils literal notranslate"><span class="pre">IncomingRequest::setValidLocales()</span></code> has been added to set
(and reset) valid locales that are set from <code class="docutils literal notranslate"><span class="pre">Config\App::$supportedLocales</span></code> setting.</p>
<section id="content-negotiation">
<h4><a class="toc-backref" href="#id6">Content Negotiation</a><a class="headerlink" href="#content-negotiation" title="Permalink to this headline"></a></h4>
<p>You can set up content negotiation to happen automatically by setting two additional settings in <strong>app/Config/App.php</strong>.
The first value tells the Request class that we do want to negotiate a locale, so simply set it to true:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">App</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="k">public</span> <span class="nx">bool</span> <span class="nv">$negotiateLocale</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Once this is enabled, the system will automatically negotiate the correct language based upon an array
of locales that you have defined in <code class="docutils literal notranslate"><span class="pre">$supportLocales</span></code>. If no match is found between the languages
that you support, and the requested language, the first item in <code class="docutils literal notranslate"><span class="pre">$supportedLocales</span></code> will be used. In
the following example, the <code class="docutils literal notranslate"><span class="pre">en</span></code> locale would be used if no match is found:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">App</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="k">public</span> <span class="k">array</span> <span class="nv">$supportedLocales</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;en&#39;</span><span class="p">,</span> <span class="s1">&#39;es&#39;</span><span class="p">,</span> <span class="s1">&#39;fr-FR&#39;</span><span class="p">];</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="in-routes">
<span id="localization-in-routes"></span><h4><a class="toc-backref" href="#id7">In Routes</a><a class="headerlink" href="#in-routes" title="Permalink to this headline"></a></h4>
<p>The second method uses a custom placeholder to detect the desired locale and set it on the Request. The
placeholder <code class="docutils literal notranslate"><span class="pre">{locale}</span></code> can be placed as a segment in your route. If present, the contents of the matching
segment will be your locale:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$routes</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;{locale}/books&#39;</span><span class="p">,</span> <span class="s1">&#39;App\Books::index&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>In this example, if the user tried to visit <strong>http://example.com/fr/books</strong>, then the locale would be
set to <code class="docutils literal notranslate"><span class="pre">fr</span></code>, assuming it was configured as a valid locale.</p>
<p>If the value doesnt match a valid locale as defined in <code class="docutils literal notranslate"><span class="pre">$supportedLocales</span></code> in <strong>app/Config/App.php</strong>, the default
locale will be used in its place, unless you set to use only the supported locales defined in the App configuration
file:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$routes</span><span class="o">-&gt;</span><span class="na">useSupportedLocalesOnly</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">useSupportedLocalesOnly()</span></code> method can be used since v4.3.0.</p>
</div>
</section>
</section>
<section id="setting-the-current-locale">
<h3><a class="toc-backref" href="#id8">Setting the Current Locale</a><a class="headerlink" href="#setting-the-current-locale" title="Permalink to this headline"></a></h3>
<p>If you want to set the locale directly, you may use
<code class="docutils literal notranslate"><span class="pre">IncomingRequest::setLocale(string</span> <span class="pre">$locale)</span></code>.
You must set supported locales in <strong>app/Config/App.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">Config</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Config\BaseConfig</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">App</span> <span class="k">extends</span> <span class="nx">BaseConfig</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="k">public</span> <span class="k">array</span> <span class="nv">$supportedLocales</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;en&#39;</span><span class="p">,</span> <span class="s1">&#39;es&#39;</span><span class="p">,</span> <span class="s1">&#39;fr-FR&#39;</span><span class="p">];</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Any attempt to set a locale not included in this array will result in
the <a class="reference internal" href="#setting-the-default-locale"><span class="std std-ref">default locale</span></a> being set.</p>
</div>
</section>
<section id="retrieving-the-current-locale">
<h3><a class="toc-backref" href="#id9">Retrieving the Current Locale</a><a class="headerlink" href="#retrieving-the-current-locale" title="Permalink to this headline"></a></h3>
<p>The current locale can always be retrieved from the IncomingRequest object, through the <code class="docutils literal notranslate"><span class="pre">getLocale()</span></code> method.
If your controller is extending <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Controller</span></code>, this will be available through <code class="docutils literal notranslate"><span class="pre">$this-&gt;request</span></code>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">UserController</span> <span class="k">extends</span> <span class="nx">BaseController</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
<span class="p">{</span>
<span class="nv">$locale</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">request</span><span class="o">-&gt;</span><span class="na">getLocale</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Alternatively, you can use the <a class="reference internal" href="../concepts/services.html"><span class="doc">Services class</span></a> to retrieve the current request:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$locale</span> <span class="o">=</span> <span class="nx">service</span><span class="p">(</span><span class="s1">&#39;request&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">getLocale</span><span class="p">();</span>
</pre></div>
</div>
</section>
</section>
<section id="language-localization">
<h2><a class="toc-backref" href="#id10">Language Localization</a><a class="headerlink" href="#language-localization" title="Permalink to this headline"></a></h2>
<section id="creating-language-files">
<h3><a class="toc-backref" href="#id11">Creating Language Files</a><a class="headerlink" href="#creating-language-files" title="Permalink to this headline"></a></h3>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The Language Files do not have namespaces.</p>
</div>
<p>Languages do not have any specific naming convention that are required. The file should be named logically to
describe the type of content it holds. For example, lets say you want to create a file containing error messages.
You might name it simply: <strong>Errors.php</strong>.</p>
<p>Within the file, you would return an array, where each element in the array has a language key and can have string to return:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;languageKey&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The actual message to be shown.&#39;</span><span class="p">,</span>
<span class="p">];</span>
</pre></div>
</div>
<p>It also support nested definition:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;languageKey&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;nested&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;key&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The actual message to be shown.&#39;</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">],</span>
<span class="p">];</span>
</pre></div>
</div>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;errorEmailMissing&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;You must submit an email address&#39;</span><span class="p">,</span>
<span class="s1">&#39;errorURLMissing&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;You must submit a URL&#39;</span><span class="p">,</span>
<span class="s1">&#39;errorUsernameMissing&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;You must submit a username&#39;</span><span class="p">,</span>
<span class="s1">&#39;nested&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;error&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;message&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;A specific error message&#39;</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">],</span>
<span class="p">];</span>
</pre></div>
</div>
</section>
<section id="basic-usage">
<h3><a class="toc-backref" href="#id12">Basic Usage</a><a class="headerlink" href="#basic-usage" title="Permalink to this headline"></a></h3>
<p>You can use the <a class="reference internal" href="../general/common_functions.html#lang" title="lang"><code class="xref php php-func docutils literal notranslate"><span class="pre">lang()</span></code></a> helper function to retrieve text from any of the language files, by passing the
filename and the language key as the first parameter, separated by a period (.). For example, to load the
<code class="docutils literal notranslate"><span class="pre">errorEmailMissing</span></code> string from the <strong>Errors.php</strong> language file, you would do the following:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Errors.errorEmailMissing&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>For nested definition, you would do the following:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Errors.nested.error.message&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>If the requested language key doesnt exist in the file for the current locale, the string will be passed
back, unchanged. In this example, it would return Errors.errorEmailMissing or Errors.nested.error.message if it didnt exist.</p>
<section id="replacing-parameters">
<h4><a class="toc-backref" href="#id13">Replacing Parameters</a><a class="headerlink" href="#replacing-parameters" title="Permalink to this headline"></a></h4>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The following functions all require the <a class="reference external" href="https://www.php.net/manual/en/book.intl.php">intl</a> extension to
be loaded on your system in order to work. If the extension is not loaded, no replacement will be attempted.
A great overview can be found over at <a class="reference external" href="https://www.sitepoint.com/localization-demystified-understanding-php-intl/">Sitepoint</a>.</p>
</div>
<p>You can pass an array of values to replace placeholders in the language string as the second parameter to the
<code class="docutils literal notranslate"><span class="pre">lang()</span></code> function. This allows for very simple number translations and formatting:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// The language file, Tests.php:</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;apples&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;I have {0, number} apples.&#39;</span><span class="p">,</span>
<span class="s1">&#39;men&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The top {1, number} men out-performed the remaining {0, number}&#39;</span><span class="p">,</span>
<span class="s1">&#39;namedApples&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;I have {number_apples, number, integer} apples.&#39;</span><span class="p">,</span>
<span class="p">];</span>
<span class="c1">// Displays &quot;I have 3 apples.&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.apples&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">]);</span>
</pre></div>
</div>
<p>The first item in the placeholder corresponds to the index of the item in the array, if its numerical:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Displays &quot;The top 23 men out-performed the remaining 20&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.men&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">23</span><span class="p">]);</span>
</pre></div>
</div>
<p>You can also use named keys to make it easier to keep things straight, if youd like:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Displays &quot;I have 3 apples.&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.namedApples&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;number_apples&#39;</span> <span class="o">=&gt;</span> <span class="mi">3</span><span class="p">]);</span>
</pre></div>
</div>
<p>Obviously, you can do more than just number replacement. According to the
<a class="reference external" href="https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classMessageFormat.html#details">official ICU docs</a> for the underlying
library, the following types of data can be replaced:</p>
<ul class="simple">
<li><p>numbers - integer, currency, percent</p></li>
<li><p>dates - short, medium, long, full</p></li>
<li><p>time - short, medium, long, full</p></li>
<li><p>spellout - spells out numbers (i.e., 34 becomes thirty-four)</p></li>
<li><p>ordinal</p></li>
<li><p>duration</p></li>
</ul>
<p>Here are a few examples:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// The language file, Tests.php</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;shortTime&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The time is now {0, time, short}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;mediumTime&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The time is now {0, time, medium}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;longTime&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The time is now {0, time, long}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;fullTime&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The time is now {0, time, full}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;shortDate&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The date is now {0, date, short}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;mediumDate&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The date is now {0, date, medium}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;longDate&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The date is now {0, date, long}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;fullDate&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The date is now {0, date, full}.&#39;</span><span class="p">,</span>
<span class="s1">&#39;spelledOut&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;34 is {0, spellout}&#39;</span><span class="p">,</span>
<span class="s1">&#39;ordinal&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The ordinal is {0, ordinal}&#39;</span><span class="p">,</span>
<span class="s1">&#39;duration&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;It has been {0, duration}&#39;</span><span class="p">,</span>
<span class="p">];</span>
<span class="c1">// Displays &quot;The time is now 11:18 PM&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.shortTime&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The time is now 11:18:50 PM&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.mediumTime&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The time is now 11:19:09 PM CDT&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.longTime&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The time is now 11:19:26 PM Central Daylight Time&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.fullTime&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The date is now 8/14/16&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.shortDate&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The date is now Aug 14, 2016&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.mediumDate&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The date is now August 14, 2016&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.longDate&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;The date is now Sunday, August 14, 2016&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.fullDate&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
<span class="c1">// Displays &quot;34 is thirty-four&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.spelledOut&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">34</span><span class="p">]);</span>
<span class="c1">// Displays &quot;It has been 408,676:24:35&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Tests.ordinal&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
</pre></div>
</div>
<p>You should be sure to read up on the MessageFormatter class and the underlying ICU formatting to get a better
idea on what capabilities it has, like performing the conditional replacement, pluralization, and more. Both of the links provided
earlier will give you an excellent idea as to the options available.</p>
</section>
<section id="specifying-locale">
<h4><a class="toc-backref" href="#id14">Specifying Locale</a><a class="headerlink" href="#specifying-locale" title="Permalink to this headline"></a></h4>
<p>To specify a different locale to be used when replacing parameters, you can pass the locale in as the
third parameter to the <code class="docutils literal notranslate"><span class="pre">lang()</span></code> function.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Displays &quot;The time is now 23:21:28 GMT-5&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Test.longTime&#39;</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()],</span> <span class="s1">&#39;ru-RU&#39;</span><span class="p">);</span>
<span class="c1">// Displays &quot;£7.41&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;{price, number, currency}&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;price&#39;</span> <span class="o">=&gt;</span> <span class="mf">7.41</span><span class="p">],</span> <span class="s1">&#39;en-GB&#39;</span><span class="p">);</span>
<span class="c1">// Displays &quot;$7.41&quot;</span>
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;{price, number, currency}&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;price&#39;</span> <span class="o">=&gt;</span> <span class="mf">7.41</span><span class="p">],</span> <span class="s1">&#39;en-US&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="nested-arrays">
<h4><a class="toc-backref" href="#id15">Nested Arrays</a><a class="headerlink" href="#nested-arrays" title="Permalink to this headline"></a></h4>
<p>Language files also allow nested arrays to make working with lists, etc… easier.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Language/en/Fruit.php</span>
<span class="k">return</span> <span class="p">[</span>
<span class="s1">&#39;list&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
<span class="s1">&#39;Apples&#39;</span><span class="p">,</span>
<span class="s1">&#39;Bananas&#39;</span><span class="p">,</span>
<span class="s1">&#39;Grapes&#39;</span><span class="p">,</span>
<span class="s1">&#39;Lemons&#39;</span><span class="p">,</span>
<span class="s1">&#39;Oranges&#39;</span><span class="p">,</span>
<span class="s1">&#39;Strawberries&#39;</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">];</span>
<span class="c1">// Displays &quot;Apples, Bananas, Grapes, Lemons, Oranges, Strawberries&quot;</span>
<span class="k">echo</span> <span class="nb">implode</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">,</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;Fruit.list&#39;</span><span class="p">));</span>
</pre></div>
</div>
</section>
</section>
<section id="language-fallback">
<h3><a class="toc-backref" href="#id16">Language Fallback</a><a class="headerlink" href="#language-fallback" title="Permalink to this headline"></a></h3>
<p>If you have a set of messages for a given locale, for instance
<strong>Language/en/app.php</strong>, you can add language variants for that locale,
each in its own folder, for instance <strong>Language/en-US/app.php</strong>.</p>
<p>You only need to provide values for those messages that would be
localized differently for that locale variant. Any missing message
definitions will be automatically pulled from the main locale settings.</p>
<p>It gets better - the localization can fall all the way back to English,
in case new messages are added to the framework and you havent had
a chance to translate them yet for your locale.</p>
<p>So, if you are using the locale <code class="docutils literal notranslate"><span class="pre">fr-CA</span></code>, then a localized
message will first be sought in the <strong>Language/fr-CA</strong> directory, then in
the <strong>Language/fr</strong> directory, and finally in the <strong>Language/en</strong> directory.</p>
</section>
<section id="message-translations">
<h3><a class="toc-backref" href="#id17">Message Translations</a><a class="headerlink" href="#message-translations" title="Permalink to this headline"></a></h3>
<p>We have an “official” set of translations in their
<a class="reference external" href="https://github.com/codeigniter4/translations">own repository</a>.</p>
<p>You could download that repository, and copy its <strong>Language</strong> folder
into your <strong>app</strong> folder. The incorporated translations will be automatically
picked up because the <code class="docutils literal notranslate"><span class="pre">App</span></code> namespace is mapped to your <strong>app</strong> folder.</p>
<p>Alternately, a better practice would be to run the following command inside your
project:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">composer require codeigniter4/translations</span>
</pre></div>
</div>
<p>The translated messages will be automatically picked
up because the translations folders get mapped appropriately.</p>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="csp.html" class="btn btn-neutral float-left" title="Content Security Policy" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="alternative_php.html" class="btn btn-neutral float-right" title="Alternate PHP Syntax for View Files" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2019-2023 CodeIgniter Foundation.
<span class="lastupdated">Last updated on Dec 28, 2023.
</span></p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(false);
});
</script>
</body>
</html>