Beispiel #1
0
 public function testCreator()
 {
     //Extract translations
     $translations1 = Gettext\Extractors\PhpCode::fromFile(__DIR__ . '/files/phpCode-example.php');
     $translations2 = Gettext\Translations::fromPhpCodefile(__DIR__ . '/files/phpCode-example.php');
     $this->assertInstanceOf('Gettext\\Translations', $translations1);
     $this->assertInstanceOf('Gettext\\Translations', $translations2);
     $this->assertEquals($translations1, $translations2);
     $result = Gettext\Generators\Po::toString($translations1);
     $this->assertEquals($result, $translations2->toPoString());
 }
Beispiel #2
0
 public function testWordpress()
 {
     //Extract translations
     $translations = Gettext\Extractors\PhpCode::fromFile(__DIR__ . '/files/wordpress-template.php');
     $this->assertInstanceOf('Gettext\\Translations', $translations);
     $po = Gettext\Generators\Po::toString($translations);
     $assert = file_get_contents(__DIR__ . '/files/wordpress-template.po');
     //remove the first 13 lines with temp info
     $po = explode("\n", $po);
     $assert = explode("\n", $assert);
     $po = implode("\n", array_splice($po, 13));
     $assert = implode("\n", array_splice($assert, 13));
     $this->assertEquals($po, $assert);
 }
Beispiel #3
0
//$file_to_translate = 'smarty.html';
$file_extension = substr($file_to_translate, stripos($file_to_translate, '.') + 1);
$file_with_translations = 'locale/' . $lang . '/' . $file_to_translate . '.php';
Gettext\Translator::setLanguage($lang);
Gettext\Translator::loadTranslations($file_with_translations);
//$entries = Gettext\Translator::getTranslationsAsEntries();
switch ($file_extension) {
    case 'js':
        $translations = Gettext\Extractors\JsCode::extract($file_to_translate);
        break;
    case 'html':
        $translations = Gettext\Extractors\Smarty::extract($file_to_translate);
        break;
    default:
        $file_extension = 'php';
        $translations = Gettext\Extractors\PhpCode::extract($file_to_translate);
        break;
}
$entries = Gettext\Translator::getTranslationsAsEntries(false, $translations);
if (isset($_POST['submit'])) {
    foreach ($_POST as $trans) {
        $context = isset($trans[0]) ? $trans[0] : '';
        $original = isset($trans[1]) ? $trans[1] : false;
        $original_translation = isset($trans[2]) ? html_entity_decode($trans[2]) : false;
        $plural = isset($trans[3]) ? $trans[3] : '';
        $plural_translation = isset($trans[4]) ? html_entity_decode($trans[4]) : false;
        $translation = $entries->find($context, $original, $plural);
        if ($translation) {
            $translation->setTranslation($original_translation);
            if ($plural) {
                $translation->setPlural($plural);
Beispiel #4
0
 /**
  * Extracts translatable strings from PHP files with xgettext.
  *
  * @param string        $rootDirectory The base root directory
  * @param array[string] $phpFiles      The relative paths to the PHP files to be parsed
  *
  * @throws \Exception Throws an \Exception in case of problems
  *
  * @return \Gettext\Translations
  */
 protected static function parseDirectoryDo_php($rootDirectory, $phpFiles)
 {
     $prefix = $rootDirectory . '/';
     $originalExtractComments = \Gettext\Extractors\PhpCode::$extractComments;
     \Gettext\Extractors\PhpCode::$extractComments = 'i18n';
     $originalFunctions = \Gettext\Extractors\PhpCode::$functions;
     \Gettext\Extractors\PhpCode::$functions['t'] = '__';
     \Gettext\Extractors\PhpCode::$functions['t2'] = 'n__';
     \Gettext\Extractors\PhpCode::$functions['tc'] = 'p__';
     try {
         $absFiles = array_map(function ($phpFile) use($prefix) {
             return $prefix . $phpFile;
         }, $phpFiles);
         $newTranslations = \Gettext\Translations::fromPhpCodeFile($absFiles);
         \Gettext\Extractors\PhpCode::$extractComments = $originalExtractComments;
         \Gettext\Extractors\PhpCode::$functions = $originalFunctions;
     } catch (\Exception $x) {
         \Gettext\Extractors\PhpCode::$extractComments = $originalExtractComments;
         \Gettext\Extractors\PhpCode::$functions = $originalFunctions;
         throw $x;
     }
     $startAt = strlen($prefix);
     foreach ($newTranslations as $newTranslation) {
         $references = $newTranslation->getReferences();
         $newTranslation->deleteReferences();
         foreach ($references as $reference) {
             $newTranslation->addReference(substr($reference[0], $startAt), $reference[1]);
         }
     }
     return $newTranslations;
 }