mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
551 lines
33 KiB
HTML
551 lines
33 KiB
HTML
|
|
|
|
<!DOCTYPE html>
|
|
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Localization — CodeIgniter4 4.0.0-alpha.1 documentation</title>
|
|
|
|
|
|
|
|
|
|
<link rel="shortcut icon" href="../_static/ci-icon.ico"/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="top" title="CodeIgniter4 4.0.0-alpha.1 documentation" href="../index.html"/>
|
|
<link rel="up" title="Library Reference" href="index.html"/>
|
|
<link rel="next" title="HTTP Messages" href="message.html"/>
|
|
<link rel="prev" title="IncomingRequest Class" href="incomingrequest.html"/>
|
|
|
|
|
|
<script src="../_static/js/modernizr.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
|
|
<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" class="icon icon-home"> CodeIgniter4
|
|
|
|
|
|
|
|
</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></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a></li>
|
|
</ul>
|
|
<ul class="current">
|
|
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Library Reference</a><ul class="current">
|
|
<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="benchmark.html">Benchmarking</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="cli.html">CLI Library</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="content_negotiation.html">Content Negotiation</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="curlrequest.html">CURLRequest Class</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="files.html">Working with Files</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="honeypot.html">Honeypot Class</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="images.html">Image Manipulation Class</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="incomingrequest.html">IncomingRequest Class</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="message.html">HTTP Messages</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="request.html">Request 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="security.html">Security Class</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="throttler.html">Throttler</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="time.html">Dates and Times</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="typography.html">Typography</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="uploaded_files.html">Working with Uploaded Files</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="uri.html">Working with URIs</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="validation.html">Validation</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a></li>
|
|
</ul>
|
|
<ul>
|
|
<li class="toctree-l1"><a class="reference internal" href="../license.html">The MIT License (MIT)</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="../changelog.html">Change Log</a></li>
|
|
</ul>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
|
|
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
|
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="../index.html">CodeIgniter4</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">Docs</a> »</li>
|
|
|
|
<li><a href="index.html">Library Reference</a> »</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>
|
|
</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>application/Language</strong> directory, with a sub-directory for each
|
|
supported language:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o">/</span><span class="nx">application</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-ci"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$defaultLocale</span> <span class="o">=</span> <span class="s1">'en'</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 fallback 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 setup 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>application/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="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 setup 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-ci"><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"><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-ci"><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">'en'</span><span class="p">,</span> <span class="s1">'es'</span><span class="p">,</span> <span class="s1">'fr-FR'</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"><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-ci"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'{locale}/books'</span><span class="p">,</span> <span class="s1">'App\Books::index'</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>In this example, if the user tried to visit <code class="docutils literal"><span class="pre">http://example.com/fr/books</span></code>, then the locale would be
|
|
set to <code class="docutils literal"><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"><span class="pre">getLocale()</span></code> method.
|
|
If your controller is extending <code class="docutils literal"><span class="pre">CodeIgniter\Controller</span></code>, this will be available through <code class="docutils literal"><span class="pre">$this->request</span></code>:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></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">-></span><span class="na">request</span><span class="o">-></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-ci"><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">'request'</span><span class="p">)</span><span class="o">-></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 the string to return:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="s1">'language_key'</span> <span class="o">=></span> <span class="s1">'The actual message to be shown.'</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-ci"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="p">[</span>
|
|
<span class="s1">'errorEmailMissing'</span> <span class="o">=></span> <span class="s1">'You must submit an email address'</span><span class="p">,</span>
|
|
<span class="s1">'errorURLMissing'</span> <span class="o">=></span> <span class="s1">'You must submit a URL'</span><span class="p">,</span>
|
|
<span class="s1">'errorUsernameMissing'</span> <span class="o">=></span> <span class="s1">'You must submit a username'</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"><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 paremeter, separated by a period (.). For example, to load the
|
|
<code class="docutils literal"><span class="pre">errorEmailMissing</span></code> string from the <code class="docutils literal"><span class="pre">Errors</span></code> language file, you would do the following:</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Errors.errorEmailMissing'</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’ 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="http://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"><span class="pre">lang()</span></code> function. This allows for very simple number translations and formatting:</p>
|
|
<div class="highlight-ci"><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">"apples"</span> <span class="o">=></span> <span class="s2">"I have {0, number} apples."</span><span class="p">,</span>
|
|
<span class="s2">"men"</span> <span class="o">=></span> <span class="s2">"I have {1, number} men out-performed the remaining {0, number}"</span><span class="p">,</span>
|
|
<span class="s2">"namedApples"</span> <span class="o">=></span> <span class="s2">"I have {number_apples, number, integer} apples."</span><span class="p">,</span>
|
|
<span class="p">];</span>
|
|
|
|
<span class="c1">// Displays "I have 3 apples."</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.apples'</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-ci"><div class="highlight"><pre><span></span><span class="c1">// Displays "The top 23 men out-performed the remaining 20"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.men'</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-ci"><div class="highlight"><pre><span></span><span class="c1">// Displays "I have 3 apples."</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s2">"Tests.namedApples"</span><span class="p">,</span> <span class="p">[</span><span class="s1">'number_apples'</span> <span class="o">=></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="http://icu-project.org/apiref/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-ci"><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">'shortTime'</span> <span class="o">=></span> <span class="s1">'The time is now {0, time, short}.'</span><span class="p">,</span>
|
|
<span class="s1">'mediumTime'</span> <span class="o">=></span> <span class="s1">'The time is now {0, time, medium}.'</span><span class="p">,</span>
|
|
<span class="s1">'longTime'</span> <span class="o">=></span> <span class="s1">'The time is now {0, time, long}.'</span><span class="p">,</span>
|
|
<span class="s1">'fullTime'</span> <span class="o">=></span> <span class="s1">'The time is now {0, time, full}.'</span><span class="p">,</span>
|
|
<span class="s1">'shortDate'</span> <span class="o">=></span> <span class="s1">'The date is now {0, date, short}.'</span><span class="p">,</span>
|
|
<span class="s1">'mediumDate'</span> <span class="o">=></span> <span class="s1">'The date is now {0, date, medium}.'</span><span class="p">,</span>
|
|
<span class="s1">'longDate'</span> <span class="o">=></span> <span class="s1">'The date is now {0, date, long}.'</span><span class="p">,</span>
|
|
<span class="s1">'fullDate'</span> <span class="o">=></span> <span class="s1">'The date is now {0, date, full}.'</span><span class="p">,</span>
|
|
<span class="s1">'spelledOut'</span> <span class="o">=></span> <span class="s1">'34 is {0, spellout}'</span><span class="p">,</span>
|
|
<span class="s1">'ordinal'</span> <span class="o">=></span> <span class="s1">'The ordinal is {0, ordinal}'</span><span class="p">,</span>
|
|
<span class="s1">'duration'</span> <span class="o">=></span> <span class="s1">'It has been {0, duration}'</span><span class="p">,</span>
|
|
<span class="p">];</span>
|
|
|
|
<span class="c1">// Displays "The time is now 11:18 PM"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.shortTime'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
<span class="c1">// Displays "The time is now 11:18:50 PM"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.mediumTime'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
<span class="c1">// Displays "The time is now 11:19:09 PM CDT"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.longTime'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
<span class="c1">// Displays "The time is now 11:19:26 PM Central Daylight Time"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.fullTime'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
|
|
<span class="c1">// Displays "The date is now 8/14/16"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.shortDate'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
<span class="c1">// Displays "The date is now Aug 14, 2016"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.mediumDate'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
<span class="c1">// Displays "The date is now August 14, 2016"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.longDate'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
<span class="c1">// Displays "The date is now Sunday, August 14, 2016"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.fullDate'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()]);</span>
|
|
|
|
<span class="c1">// Displays "34 is thirty-four"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.spelledOut'</span><span class="p">,</span> <span class="p">[</span><span class="mi">34</span><span class="p">]);</span>
|
|
|
|
<span class="c1">// Displays "It has been 408,676:24:35"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Tests.ordinal'</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 permorming 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"><span class="pre">lang()</span></code> method.</p>
|
|
<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="c1">// Displays "The time is now 23:21:28 GMT-5"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Test.longTime'</span><span class="p">,</span> <span class="p">[</span><span class="nb">time</span><span class="p">()],</span> <span class="s1">'ru-RU'</span><span class="p">);</span>
|
|
|
|
<span class="c1">// Displays "£7.41"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'{price, number, currency}'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'price'</span> <span class="o">=></span> <span class="mf">7.41</span><span class="p">],</span> <span class="s1">'en-GB'</span><span class="p">);</span>
|
|
<span class="c1">// Displays "$7.41"</span>
|
|
<span class="k">echo</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'{price, number, currency}'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'price'</span> <span class="o">=></span> <span class="mf">7.41</span><span class="p">],</span> <span class="s1">'en-US'</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-ci"><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">'list'</span> <span class="o">=></span> <span class="p">[</span>
|
|
<span class="s1">'Apples'</span><span class="p">,</span>
|
|
<span class="s1">'Bananas'</span><span class="p">,</span>
|
|
<span class="s1">'Grapes'</span><span class="p">,</span>
|
|
<span class="s1">'Lemons'</span><span class="p">,</span>
|
|
<span class="s1">'Oranges'</span><span class="p">,</span>
|
|
<span class="s1">'Strawberries'</span>
|
|
<span class="p">]</span>
|
|
<span class="p">];</span>
|
|
|
|
<span class="c1">// Displays "Apples, Bananas, Grapes, Lemons, Oranges, Strawberries"</span>
|
|
<span class="k">echo</span> <span class="nb">implode</span><span class="p">(</span><span class="s1">', '</span><span class="p">,</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">'Fruit.list'</span><span class="p">));</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
<div class="articleComments">
|
|
|
|
</div>
|
|
</div>
|
|
<footer>
|
|
|
|
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
|
|
|
<a href="message.html" class="btn btn-neutral float-right" title="HTTP Messages" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
|
|
|
|
|
|
<a href="incomingrequest.html" class="btn btn-neutral" title="IncomingRequest Class" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
|
|
|
</div>
|
|
|
|
|
|
<hr/>
|
|
|
|
<div role="contentinfo">
|
|
<p>
|
|
© Copyright 2014-2018 British Columbia Institute of Technology.
|
|
Last updated on Sep 23, 2018.
|
|
|
|
</p>
|
|
</div>
|
|
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/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">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT:'../',
|
|
VERSION:'4.0.0-alpha.1',
|
|
COLLAPSE_INDEX:false,
|
|
FILE_SUFFIX:'.html',
|
|
HAS_SOURCE: false,
|
|
SOURCELINK_SUFFIX: ''
|
|
};
|
|
</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/js/theme.js"></script>
|
|
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
jQuery(function () {
|
|
SphinxRtdTheme.StickyNav.enable();
|
|
});
|
|
</script>
|
|
|
|
|
|
</body>
|
|
</html> |