PHP code example of niirrty / niirrty.translation

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

    

niirrty / niirrty.translation example snippets




use \Niirrty\Locale\Locale;
use \Niirrty\Translation\Sources\PHPFileSource;
use \Niirrty\Translation\Translator;

try
{
    // Init the locale
    $locale = Locale::Create(
        // The fallback locale if no other was found
        new Locale( 'de', 'de', 'UTF-8' ),
        // Check also the URL path for an locale or language part?
        true,
        // This are the names of the parameters, accepted from $_POST, $_GET and $_SESSION
        [ 'lc', 'locale', 'language', 'lang', 'lng' ]
    );

    // Init the translator instance with defined locale
    $translator = new Translator( $locale );

    // Define the templates source directory. There is also a JSONFileSource and a XMLFileSource
    $translatorSource = new PHPFileSource( __DIR__ . '/translations', $locale );
    // ...and add the source to template engine
    $translator->addSource( $translatorSource );

    // If you want access the Translator instance globally
    $translator->setAsGlobalInstance();

    // Example output
    echo Translator::GetInstance()->read( 'Hello1', 'MyApp', 'Hello people!' ), "\n",
         $translator->read( 'NiceTM', 'MyApp', 'NiceToMeetYouM!' );

}
catch ( \Throwable $ex )
{
    // TODO: Do some error handling
}



return [
    'Text 1' => 'Ein Beispieltext',
    'Text 2' => 'Ein anderer Beispieltext',
    'weekdaysList' => [ 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag' ],
    'weekdaysDict' => [ 'Mo' => 'Montag', 'Di' => 'Dienstag', 'Mi' => 'Mittwoch', 'Do' => 'Donnerstag',
                        'Fr' => 'Freitag', 'Sa' => 'Samstag', 'So' => 'Sonntag' ]
];
json
{
   "p": ">=8.1",
      "niirrty/niirrty.translation": "~0.6"
   }
}

+ translations
   - de.php
   - de_DE.php
   - en.php
   - fr_FR.php