Skip to content

Array Usage

dvlpr1996 edited this page May 26, 2024 · 2 revisions

Localization with Array

  • create lang directory
  • Within lang directory for each language supported by your app create subdirectories
 lang/
   en/
     app.php
     ...
 
   fa/
     app.php
     ...

All language files return an Associative Array:

<?php

return [
    'hi' => 'Welcome to our website',
    'welcome' => 'Welcome to our website dear :name' // with $replacement param
];
use dvlpr1996\PhpLocalization\Localization;

$localization = new Localization([
    'driver' => 'array',
    'langDir' => __DIR__ . '/lang/',
    'defaultLang' => 'en',
    'fallBackLang' => 'fa'
]);

//lang method Translate the given message

echo $localization->lang($key, $replacement);
  • string $key parameter => key with dot notation (fileName . arrayKey)
  • array $replacement parameter (optional) => to define placeholders in translation strings (:placeholdersName)

for example

echo $localization->lang('app.welcome', [
  ':Name' => 'john'
]);

// Welcome to our website dear John

If $replacement parameter is uppercase, lowercase or pascal case, It has an effect on the way the string output is displayed

Clone this wiki locally