- Ness
- Helpers
- Language
- Helpers
You can include this library to your code file by typing the below code;
use Ness\Helpers\Language
About Language Helper #top
Ness PHP Framework has a helper library to help you creating multi-language applications. You can create easily and quickly applications with lots of language files and switch between languages by Session/Cookie/Region or by choice. You need to write a logic to switch between language files. This tutorial will guide you to create a multi-language application and show you all available methods in Helper/Language namespace.
Example Language File #top
Add a new language file located in [project_folder]/Application/Language.
Let's say we are creating a language support for 'English' and 'Turkish'; add new files in the language path:
'lang.en.php'.
<?php
/**
* EXAMPLE LANGUAGE FILE FOR OUR APPLICATION
* DEFAULT TERMS FOR LANGUAGE: ENGLISH.
*/
$panel = array("lblDash" => "Dashboard",
"lblLogin" => "Login",
"lblCreate" => "Create",
"lblDelete" => "Delete",
"lblUpdate"=>"Update");
'lang.tr.php'.
<?php
/**
* EXAMPLE LANGUAGE FILE FOR OUR APPLICATION
* DEFAULT TERMS FOR LANGUAGE: TURKISH.
*/
$panel = array("lblDash" => "Ana Sayfa",
"lblLogin" => "Giris",
"lblCreate" => "Olustur",
"lblDelete" => "Sil",
"lblUpdate"=>"Guncelle");
We have create 2 language files for our application; Assume that we have a property called $user_data and stored language preference in $user_data['lang'];
<?php
use Ness\Controller as myController;
use Ness\Helpers\Language;
class indexController extends myController
{
public function indexAction($param = 0)
{
/**
* Test variable that stores user data in
*/
$user_data = array("username"=>"sinansalih0",
"lang"=>"tr",
"login_time"=>"12:33:25 - 06/06/2019");
//Create Language class instance
$lang_setter = new Language();
//Language code and file name to make a switch logic;
$lang_name = 'lang.'.$user_data['lang'].'.php';
//create array with strings get from language file;
$labels_panel = $lang_setter->setLanguage($lang_name)
->setVariable("panel")
->bindLanguage(); //Now all data in panel variable will
//be loaded to $labels_panel related to language
//preferation of user
//Test the language class;
echo $labels_panel['lblDash'];
}
}
Result: Ana Sayfa
if we change the $user_data['lang'] = 'en';
result will be:
Dashboard.
Methods
isAvailable
Language::isAvailable(string $prm);
This static method will return true a language file is found in 'Language' path | ||
Return | Boolean | |
Parameters | $prm String; language file name |
setLanguage
$lang_helper = new Language();
$lang_helper->setLanguage(string $prm);
Set a language file to get the contents | ||
Return | $this | |
Parameters | $prm String; language file name |
setVariable
$lang_helper = new Language();
$lang_helper->setLanguage('default.en.php');
$lang_helper->setVariable(string $prm);
Set variable name in language file; this is used generally to seperate your values in arrays to make them update quickly. | ||
Return | $this | |
Parameters | $prm String; Variable name in language file |
bindLanguage
$lang_helper = new Language();
$lang_helper->setLanguage('default.en.php');
$lang_helper->setVariable('homepage_labels');
$labels = $lang_helper->bindLanguage();
Get values stored in language file ('default.en.php') and in array ('homepage_labels'); | ||
Return | Array | |
Parameters | None |