You are very much on track, except for one bit, your data and code are intertwined, which is not a good form of design. Different people use various approaches for handling i18N. I prefer using .ini files, you can have the data in different INI files and even add sections if you want. Using your example, you can do:
-- en.ini
PAGE_TITLE = 'My website page title'
HEADER_TITLE = 'My website header title'
SITE_NAME' = 'My Website'
SLOGAN = 'My slogan here'
HEADING = 'Heading'
-- it.ini
PAGE_TITLE = 'Il titolo della mia pagina'
HEADER_TITLE = 'Il mio titolo'
SITE_NAME = 'Il nome del mio sito'
SLOGAN = 'Uno slogan'
HEADING = 'Heading'
Then your code can be rewritten as :
<?php
class Language {
private $UserLng;
private $langSelected;
public $lang = array();
public function __construct($userLanguage){
$this->UserLng = $userLanguage;
//construct lang file
$langFile = '/path/to/ini/files/'. $this->UserLng . '.ini';
if(!file_exists($langFile)){
throw new Execption("Language could not be loaded"); //or default to a language
}
$this->lang = parse_ini_file($langFile);
}
public function userLanguage(){
return $this->lang;
}
}