PHP code example of carry0987 / i18n

1. Go to this page and download the library: Download carry0987/i18n 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/ */

    

carry0987 / i18n example snippets




use carry0987\I18n\I18n;

$i18n = new I18n([
    'useAutoDetect' => true, // By default, the language is automatically detected
    'langFilePath' => 'path/to/lang', 
    'cachePath' => 'path/to/cache'
]);

// Use file name and key to get the translation value
echo $i18n->fetch('greeting.hello'); // Outputs: "Hello"

// Get multiple translations in the current language
$translations = $i18n->fetchList(); // Empty parameter returns all translations

$config = array(
    'useAutoDetect' => true,
    //...
    'cookie' => array(
        'name' => 'lang',
        'expire' => time()+864000,
        'path' => '/',
        'domain' => '',
        'secure' => true,
        'httponly' => true
    )
);

$i18n = new I18n([
    'useAutoDetect' => false, // Set useAutoDetect to false to disable automatic language detection
    //...
]);
// Initialize the language settings
$i18n->initialize('en_US'); // 'en_US' is the language code

$i18n = new I18n([
    'allowedFiles' => ['general', 'greeting'], // Set the list of language files to be loaded
    //...
]);

// Define aliases for languages
$i18n->setLangAlias(array('en_US' => 'English', 'zh_TW' => '繁體中文'));

// Fetch the list of languages with their aliases
$aliasedLangList = $i18n->fetchLangList();
// Outputs: array('en_US' => 'English', 'zh_TW' => '繁體中文')

.
├── cache
├── lang
│   ├── en_US
│   │   ├── general.json
│   │   └── greeting.json
│   └── zh_TW
│       ├── general.json
│       └── greeting.json
├── src
│   ├── /...
│   └── I18n.php
└── vendor