Hi,
I wish to do a multi-langague. I have this in my mind.
Lots of files for each language with $lang['vars'], but I have one problem. How can I make my functions get the $lang['var'] from a file?
Thanks!
Hi,
I wish to do a multi-langague. I have this in my mind.
Lots of files for each language with $lang['vars'], but I have one problem. How can I make my functions get the $lang['var'] from a file?
Thanks!
Have one "language" file that does nothing but declare values for this $lang array. Then, use a function like [man]require_once/man to load it:
require_once 'lang/spanish.php';
echo $lang['greeting']; // ¡Bienvenido!
Hi, still I'd need to do the require_once inside each one of the functions, right? Or is there another way?
Just a little question can the functions load $_COOKIE info?
Thanks anyways.
What you'd do is use require_once() at the top of your page, and then inside functions you'd have to initialize the "global" variable $lang like so:
<?PHP
require_once 'langs/spanish.php';
function sayHello() {
global $lang;
echo $lang['greeting']; // ¡Bienvenido!
}
To answer your other question, at least what I believe is your question, yes. the $_COOKIE array is a superglobal array, meaning it can be accessed from any scope (inside functions, classes, etc.).
Right, but still I'd need to do global $lang inside each function, isn't there any other way?
I want to connect all functions in one class, but they're from different files, how can I make em all share $this info and the $lang?
And thanks again for the help!
Here is one way to do it, if you use objects it doesn't have to be exactly this, there are quite a few similar OOP mplementations, google for it
// in some config file setup your language
define( 'SITE_LANGUAGE', 'Japanese' ) ;
// Your language manager
class LanguageManager
{
static $lang = null ;
public static function getLang()
{
if( !self::$lang )
{
switch( SITE_LANGUAGE )
{
case 'japanese':
require_once( 'JapaneseLanguage.php' ) ;
self::$lang = new JapaneseLanguage() ;
case 'spanish':
require_once( 'SpanishLanguage.php' ) ;
self::$lang = new SpanishLanguage() ;
}
}
retrn self::$lang() ;
}
}
// The following might be overkill, you could also just have getLang() return an
//array, or have the class concrete language class be
// composed of constants instead
// We use an interface to make sure all languages have all
// the required functionality
interface Language
{
public function greeting() ;
public function goodbye() ;
public function insult() ;
}
class JapaneseLanguage implements Language
{
public function greeting()
{
return 'Konichiwa' ;
}
public function goodbye()
{
return 'whatever goodbye is in japanese' ;
}
}
and this is how you would use it
define
function sayHello() {
echo LanguageManager::getLang()->greeting() ;
// or
$lang = LanguageManager::getLang() ;
echo $lang->greeting() ;
}
If you put it in a class
class Thingy
{
protected $lang ;
public function __construct()
{
$this->lang = LanguageManager::getLang() ;
}
public function SayHello()
{
echo $this->lang->greeting() ;
}
}
You could also give the [man]gettext[/man] extension a look.
Something similar could be done without the extension, though. Each string that has translations appears in the code as
_("Do you speak English?")
(instead of just "Do you speak English?"). Then the _() function (yes, "" is a valid function name) looks roughly like this:
function _($string)
{
global $lang; // which has already been included
if(isset($lang[$string])) return $lang[$string];
else return $string; // use the original for a default.
}
In the example above, the German lang file would declare an array
$lang = array(
//....
"Do you speak English?" => "Sprechen Sie Englisch?"
//...
);