PaperBag/application/Translator.php

170 lines
4.8 KiB
PHP

<?php
class Translator {
public function __construct(){
$this->language = "nb";
$this->country = "NO";
$this->charset = "utf8";
$this->locale = $this->language."_".$this->country.".".$this->charset;
if (defined('LC_MESSAGES')) {
setlocale(LC_MESSAGES, $this->locale);
bindtextdomain("domain1", __dir__."/../locale/");
} else {
echo "IS IN THE ELSE - PART";
}
textdomain("domain1");
}
}
/*
use Gettext\Loader\PoLoader;
class Translator {
private static $instance = null;
private $translations;
private $language;
private $country;
private $charset;
private $locale;
private function __construct($lang = "nb", $contry = "NO", $charset = "utf8"){
$this->language = $lang;
$this->country = $contry;
$this->charset = $charset;
$this->locale = $this->language."_".$this->country.".".$this->charset;
//import from a .po file:
$poloader = new PoLoader();
$this->translations = $poloader->loadFile('locale/'.$this->language.'.po');
}
public static function getInstance(): Translator {
if(self::$instance == null){
self::$instance = new Translator();
}
return self::$instance;
}
static public function translate($string, $domain = "messages"): string {
$trans = self::getInstance();
return $trans->translations;
}
static public function getBrowserSupportedLanguage(){
$language = null;
$input = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$supported_languages = ["en", "no"];
$browserLanguages = explode(",", str_replace(' ', '', strtolower($input)));
foreach ($browserLanguages as $languageString){
// Stop looking for languages if one is found
if(!empty($language)){ break; }
// Check for language in format "en_US"
$stringParts = explode('-', $languageString);
// If not in that format, check for "en;q=0.9" format
if(count($stringParts) <= 1){
$stringParts = explode(';', $languageString);
}
// If whichever format, check if the first key is a supported language
if(count($stringParts) > 1 && in_array($stringParts[0], $supported_languages)){
$language = $stringParts[0];
}
// If in neither format, check the whole string
elseif(in_array($languageString, $supported_languages)){
$language = $languageString;
}
}
// If no language found, set the first one in the supported-list
if(empty($language)){
$language = $supported_languages[0];
}
return $language;
}
}
*/
use Gettext\Scanner\PhpScanner;
use Gettext\Translations;
use Gettext\Generator\PoGenerator;
use Gettext\Generator\MoGenerator;
class TranslatorScanning {
public $scanner;
public function __construct($domain = "default"){
$phpScanner = new PhpScanner(
Translations::create($domain)
);
//Set a default domain, so any translations with no domain specified, will be added to that domain
$phpScanner->setDefaultDomain($domain);
//Extract all comments starting with 'i18n:' and 'Translators:'
$phpScanner->extractCommentsStartingWith('i18n:', 'Translators:');
//Scan files
foreach (glob(__DIR__.'/../www/{*,*/*,*/*/*}.php', GLOB_BRACE) as $file) {
// echo $file."<br>\n";
$phpScanner->scanFile($file);
}
$this->scanner = $phpScanner;
}
public function save(){
//Save the translations in .po files
$generator = new PoGenerator();
foreach ($this->phpScanner->getTranslations() as $domain => $translations) {
echo "Generate file: ".__DIR__."/../www/{$domain}.po<br>\n";
$generator->generateFile($translations, __DIR__."/../www/{$domain}.po");
}
}
public static function compiler(){
$loader = new PoLoader();
//From a file
$translations = $loader->loadFile(__DIR__."/../www/domain1.po");
$generator = new MoGenerator();
$generator->generateFile($translations, __DIR__."/../www/domain1.mo");
// From a string
// $string = file_get_contents('locales2/en.po');
// $translations = $loader->loadString($string);
}
}
// Resources: https://packagist.org/packages/gettext/gettext
// Resources: https://www.sitepoint.com/easy-multi-language-twig-apps-with-gettext/
function __($string, ...$values): string {
if(DEBUG){
logTranslationKey($string);
}
return sprintf( _($string), $values);
}
function logTranslationKey($string){
file_put_contents("translationkeys.txt", file_get_contents("translationkeys.txt") . "\n" . $string);
}