Skip to content

Json Usage

dvlpr1996 edited this page May 26, 2024 · 3 revisions

Localization with Json

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

All language files return an Associative Array:

{
    "hi" : "Welcome to our website",
    "welcome" : "Welcome to our website dear :name"
}
use dvlpr1996\PhpLocalization\Localization;

$localization = new Localization([
    'driver' => 'json',
    '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 (jsonKey)
  • array $replacement parameter (optional) => to define placeholders in translation strings (:placeholdersName)

for example

echo $localization->lang('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