PHP code example of xfra35 / f3-multilang

1. Go to this page and download the library: Download xfra35/f3-multilang library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xfra35 / f3-multilang example snippets


$f3->set('MULTILANG.languages',array(
  'en' => 'en-GB,en-US,en',
  'ja' => 'ja-JP,ja',
  'es' => 'es-ES,es'
));

$f3->config('config.ini');
Multilang::instance();
$f3->run();

$f3->set('MULTILANG.rules',array(
  'es' => array(
    'terms' => '/terminos-y-condiciones'
  )
));

$f3->reroute('@contact'); // OK => reroute to /xx/contact where xx is the current language

$f3->reroute('/en/contact'); // OK
$f3->reroute('/contact'); // Error => 404 Not Found

$ml->reroute('/en/contact'); // OK
$ml->reroute('/contact'); // OK => reroute to /xx/contact where xx is the current language

$f3->set('ONREROUTE',function($url,$permanent) use($f3,$ml){
  $f3->clear('ONREROUTE');
  $ml->reroute($url,$permanent);
});

// then in your controller, existing reroutes will keep on working:
$f3->reroute('/contact'); // OK => reroute to /xx/contact where xx is the current language

$ml = Multilang::instance();

echo $ml->current;// ja

echo $ml->primary;// en

echo $ml->auto;//FALSE

echo $ml->passthru;//FALSE

echo $ml->locale();// en_GB.UTF-8

// on a Danish route (with intl)
echo $ml->displayLanguage('fr');// fransk

// on a Russian route (with intl)
echo $ml->displayLanguage('fr');// французский

// on any route (without intl)
echo $ml->displayLanguage('fr');// French

// on a Danish route (with intl)
echo $ml->displayCountry('ru');// Rusland

// on a Russian route (with intl)
echo $ml->displayCountry('ru');// Россия

// on any route (without intl)
echo $ml->displayCountry('ru');// Russia

$ml->languages();// array('en','ja','es')

$ml->locales();
/* array(
    'en' => 'en-GB,en-US,en',
    'ja' => 'ja-JP,ja',
    'es' => 'es-ES,es'
)*/

$ml->aliases();// array('terms','blog','captcha')

$ml->isLocalized('terms');// TRUE (current language)
$ml->isLocalized('terms','es');// TRUE
$ml->isLocalized('blog','es');// FALSE (excluded for Spanish)
$ml->isLocalized('captcha');// FALSE (global)
$ml->isLocalized('foo');// FALSE (non-existent route)

$ml->isGlobal('captcha');// TRUE

echo $ml->alias('terms',NULL,'es');// /es/terminos-y-condiciones [local route]
echo $ml->alias('captcha');// /captcha [global route]

$ml->reroute('/en/contact'); // OK
$ml->reroute('/contact'); // OK => reroute to /xx/contact where xx is the current language
ini
[MULTILANG.languages]
en = en-GB, en-US, en
ja = ja-JP, ja
es = es-ES, es
ini
[MULTILANG.rules.es]
blog = FALSE
ini
[MULTILANG]
global = captcha
;could also be an array:
global = alias1, alias2, alias3, /admin, /foo/bar
ini
[MULTILANG.rules.en]
contact = /contact
blog = /contact