PHP code example of locomotivemtl / charcoal-translation

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

    

locomotivemtl / charcoal-translation example snippets


use \Charcoal\Translation\TranslationConfig;

$translation_config = TranslationConfig::instance();

// Set the list of available languages
$translation_config->set_available_langs([
    'en' => [],
    'fr' => []
]);

// Set the default language to "English" ("en")
$translation_config->set_default_lang('en');

// Set the current language to French
$translation_config->set_lang('fr');

use \Charcoal\Translation\Catalog;

$catalog = Catalog::instance();
$catalog->add_resource('myproject.csv');
echo $catalog->tr('my string');

// Add a custom string..
$catalog->add_translation('custom string', [
    'en' => 'Custom string',
    'fr' => 'Chaîne aléatoire'
]);
ech $catalog->tr('custom string');

// Let's assume the default language has been set to 'en'...

use \Charcoal\Translation\TranslationString;

$str = new TranslationString([
    'fr' => 'foo',
    'en' => 'bar'
]);

// All the following examples output "bar"
echo $str;
echo $str->en();
echo $str->val('en');

// All the following examples output "foo"
echo $str->fr();
echo $str->set_lang('fr')->val();
echo $str->val('fr');
//