PHP code example of sagittariusx / beluga.translation

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

    

sagittariusx / beluga.translation example snippets


use \Beluga\Tranlation\Locale;

Locale::Create(
   // The fallback locale if no other was found
   new Locale( 'de', 'AT', '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
   [ 'locale', 'language', 'lang' ]
)
   ->registerAsGlobalInstance();

if ( Locale::HasGlobalInstance() )
{
   $locale = Locale::GetGlobalInstance();
}
else
{
   // Create the locale
   //$locale = Locale::Create( … )->registerAsGlobalInstance();
}


use \Beluga\Translation\{Locale,Translator};
use \Beluga\Translation\Source\ArraySource;

class Foo
{

   /**
    * @type \Beluga\Translation\Translator
    */
   private $trans;
   
   public function __construct( Locale $locale = null )
   {
   
      $_locale = null;
   
      if ( ! \is_null( $locale ) )
      {
         $_locale = $locale
      }
      
      else if ( Locale::HasGlobalInstance() )
      {
         $_locale = Locale::GetGlobalInstance();
      }
      
      if ( ! \is_null( $_locale ) )
      {
         $source = ArraySource::LoadFromFolder( __DIR__ . '/i18n', $_locale, false );
         $this->trans = new Translator( $source )
      }
      
   }
   
   public function getTranslation( $mainLanguageText )
   {
      
      if ( ! ( $this->trans instanceof ITranlator ) )
      {
         return $mainLanguageText;
      }
      
      return $this->trans->translateByText( $mainLanguageText );
      
   }
   
   
}