Beispiel #1
0
 function test_translations_merge()
 {
     $host = new Translations();
     $host->add_entry(new Translation_Entry(array('singular' => 'pink')));
     $host->add_entry(new Translation_Entry(array('singular' => 'green')));
     $guest = new Translations();
     $guest->add_entry(new Translation_Entry(array('singular' => 'green')));
     $guest->add_entry(new Translation_Entry(array('singular' => 'red')));
     $host->merge_with($guest);
     $this->assertEquals(3, count($host->entries));
     $this->assertEquals(array(), array_diff(array('pink', 'green', 'red'), array_keys($host->entries)));
 }
 function test_digit_and_merge()
 {
     $entry_digit_1 = new Translation_Entry(array('singular' => 1, 'translations' => array('1')));
     $entry_digit_2 = new Translation_Entry(array('singular' => 2, 'translations' => array('2')));
     $domain = new Translations();
     $domain->add_entry($entry_digit_1);
     $domain->add_entry($entry_digit_2);
     $dummy_translation = new Translations();
     $this->assertEquals('1', $domain->translate('1'));
     $domain->merge_with($dummy_translation);
     $this->assertEquals('1', $domain->translate('1'));
 }
Beispiel #3
0
 public function read_translations_from_file($file_name, $project = null)
 {
     if (is_null($project)) {
         return false;
     }
     $translations = $this->read_originals_from_file($file_name);
     if (!$translations) {
         return false;
     }
     $originals = GP::$original->by_project_id($project->id);
     $new_translations = new Translations();
     foreach ($translations->entries as $key => $entry) {
         // we have been using read_originals_from_file to parse the file
         // so we need to swap singular and translation
         if ($entry->context == $entry->singular) {
             $entry->translations = array();
         } else {
             $entry->translations = array($entry->singular);
         }
         $entry->singular = null;
         foreach ($originals as $original) {
             if ($original->context == $entry->context) {
                 $entry->singular = $original->singular;
                 break;
             }
         }
         if (!$entry->singular) {
             error_log(sprintf(__('Missing context %s in project #%d', 'glotpress'), $entry->context, $project->id));
             continue;
         }
         $new_translations->add_entry($entry);
     }
     return $new_translations;
 }
 function create_translations_with($entries)
 {
     $translations = new Translations();
     foreach ($entries as $entry) {
         $translations->add_entry($entry);
     }
     return $translations;
 }
Beispiel #5
0
 function import($translations)
 {
     $this->set_memory_limit('256M');
     if (!isset($this->project) || !$this->project) {
         $this->project = GP::$project->get($this->project_id);
     }
     $locale = GP_Locales::by_slug($this->locale);
     $user = wp_get_current_user();
     $current_translations_list = GP::$translation->for_translation($this->project, $this, 'no-limit', array('status' => 'current', 'translated' => 'yes'));
     $current_translations = new Translations();
     foreach ($current_translations_list as $entry) {
         $current_translations->add_entry($entry);
     }
     unset($current_translations_list);
     $translations_added = 0;
     foreach ($translations->entries as $entry) {
         if (empty($entry->translations)) {
             continue;
         }
         $is_fuzzy = in_array('fuzzy', $entry->flags);
         if ($is_fuzzy && !apply_filters('gp_translation_set_import_fuzzy_translations', true, $entry, $translations)) {
             continue;
         }
         $create = false;
         if ($translated = $current_translations->translate_entry($entry)) {
             // we have the same string translated
             // create a new one if they don't match
             $entry->original_id = $translated->original_id;
             $translated_is_different = array_pad($entry->translations, $locale->nplurals, null) != $translated->translations;
             $create = apply_filters('gp_translation_set_import_over_existing', $translated_is_different);
         } else {
             // we don't have the string translated, let's see if the original is there
             $original = GP::$original->by_project_id_and_entry($this->project->id, $entry, '+active');
             if ($original) {
                 $entry->original_id = $original->id;
                 $create = true;
             }
         }
         if ($create) {
             if ($user) {
                 $entry->user_id = $user->ID;
             }
             $entry->translation_set_id = $this->id;
             $entry->status = apply_filters('gp_translation_set_import_status', $is_fuzzy ? 'fuzzy' : 'current');
             // check for errors
             $translation = GP::$translation->create($entry);
             $translation->set_status($entry->status);
             $translations_added += 1;
         }
     }
     gp_clean_translation_set_cache($this->id);
     do_action('gp_translations_imported', $this->id);
     return $translations_added;
 }
Beispiel #6
0
 function test_multiple_imports_multiple_singulars()
 {
     $originals = array(array('singular' => 'Good morning'), array('singular' => 'Good evening'));
     $translations1 = new Translations();
     $translations1->add_entry(new Translation_Entry(array('singular' => $originals[0]['singular'], 'translations' => array('Guten Morgen'))));
     $translations2 = new Translations();
     $translations2->add_entry(new Translation_Entry(array('singular' => $originals[1]['singular'], 'translations' => array('Guten Abend'))));
     $this->_verify_multiple_imports($originals, array(array('status' => 'current', 'translations' => $translations1, 'counts' => array('all_count' => 2, 'current_count' => 1, 'untranslated_count' => 1, 'waiting_count' => 0)), array('status' => 'current', 'translations' => $translations2, 'counts' => array('all_count' => 2, 'current_count' => 2, 'untranslated_count' => 0, 'waiting_count' => 0))));
     $this->_verify_multiple_imports($originals, array(array('status' => 'current', 'translations' => $translations1, 'counts' => array('all_count' => 2, 'current_count' => 1, 'untranslated_count' => 1, 'waiting_count' => 0)), array('status' => 'waiting', 'translations' => $translations2, 'counts' => array('all_count' => 2, 'current_count' => 1, 'untranslated_count' => 1, 'waiting_count' => 1))));
     $this->_verify_multiple_imports($originals, array(array('status' => 'waiting', 'translations' => $translations1, 'counts' => array('all_count' => 2, 'current_count' => 0, 'untranslated_count' => 2, 'waiting_count' => 1)), array('status' => 'waiting', 'translations' => $translations2, 'counts' => array('all_count' => 2, 'current_count' => 0, 'untranslated_count' => 2, 'waiting_count' => 2))));
     $this->_verify_multiple_imports($originals, array(array('status' => 'waiting', 'translations' => $translations1, 'counts' => array('all_count' => 2, 'current_count' => 0, 'untranslated_count' => 2, 'waiting_count' => 1)), array('status' => 'current', 'translations' => $translations1, 'counts' => array('all_count' => 2, 'current_count' => 1, 'untranslated_count' => 1, 'waiting_count' => 0))));
 }
 function test_filter_translation_set_import_over_existing()
 {
     $set = $this->factory->translation_set->create_with_project_and_locale();
     $original = $this->factory->original->create(array('project_id' => $set->project->id, 'status' => '+active', 'singular' => 'A string'));
     $translation = $this->factory->translation->create(array('translation_set_id' => $set->id, 'original_id' => $original->id, 'translations' => array('baba'), 'status' => 'current'));
     $translations_for_import = new Translations();
     $translations_for_import->add_entry(array('singular' => 'A string', 'translations' => array('abab')));
     add_filter('translation_set_import_over_existing', '__return_false');
     $translations_added = $set->import($translations_for_import);
     remove_filter('translation_set_import_over_existing', '__return_false');
     $this->assertEquals($translations_added, 0);
 }
Beispiel #8
0
 public function read_originals_from_file($file_name)
 {
     $errors = libxml_use_internal_errors(true);
     $data = simplexml_load_string(file_get_contents($file_name));
     libxml_use_internal_errors($errors);
     if (!is_object($data)) {
         return false;
     }
     $entries = new Translations();
     foreach ($data->string as $string) {
         if (isset($string['translatable']) && 'false' == $string['translatable']) {
             continue;
         }
         $entry = new Translation_Entry();
         $entry->context = (string) $string['name'];
         $entry->singular = $this->unescape((string) $string[0]);
         $entry->translations = array();
         if (isset($string['comment']) && $string['comment']) {
             $entry->extracted_comments = $string['comment'];
         }
         $entries->add_entry($entry);
     }
     foreach ($data->{'string-array'} as $string_array) {
         if (isset($string_array['translatable']) && 'false' == $string_array['translatable']) {
             continue;
         }
         $array_name = (string) $string_array['name'];
         $item_index = 0;
         foreach ($string_array->item as $item) {
             $entry = new Translation_Entry();
             $entry->context = $array_name . "[{$item_index}]";
             $entry->singular = $this->unescape($item[0]);
             $entry->translations = array();
             $entries->add_entry($entry);
             $item_index++;
         }
     }
     return $entries;
 }
Beispiel #9
0
 function test_translate_plural()
 {
     $entry_incomplete = new Translation_Entry(array('singular' => 'baba', 'plural' => 'babas', 'translations' => array('babax')));
     $entry_toomany = new Translation_Entry(array('singular' => 'wink', 'plural' => 'winks', 'translations' => array('winki', 'winka', 'winko')));
     $entry_2 = new Translation_Entry(array('singular' => 'dyado', 'plural' => 'dyados', 'translations' => array('dyadox', 'dyadoy')));
     $domain = new Translations();
     $domain->add_entry($entry_incomplete);
     $domain->add_entry($entry_toomany);
     $domain->add_entry($entry_2);
     $this->assertEquals('other', $domain->translate_plural('other', 'others', 1));
     $this->assertEquals('others', $domain->translate_plural('other', 'others', 111));
     // too few translations + cont logic
     $this->assertEquals('babas', $domain->translate_plural('baba', 'babas', 2));
     $this->assertEquals('babas', $domain->translate_plural('baba', 'babas', 0));
     $this->assertEquals('babas', $domain->translate_plural('baba', 'babas', -1));
     $this->assertEquals('babas', $domain->translate_plural('baba', 'babas', 999));
     // proper
     $this->assertEquals('dyadox', $domain->translate_plural('dyado', 'dyados', 1));
     $this->assertEquals('dyadoy', $domain->translate_plural('dyado', 'dyados', 0));
     $this->assertEquals('dyadoy', $domain->translate_plural('dyado', 'dyados', 18881));
     $this->assertEquals('dyadoy', $domain->translate_plural('dyado', 'dyados', -18881));
 }
Beispiel #10
0
 function import($translations)
 {
     @ini_set('memory_limit', '256M');
     if (!isset($this->project) || !$this->project) {
         $this->project = GP::$project->get($this->project_id);
     }
     $locale = GP_Locales::by_slug($this->locale);
     $current_translations_list = GP::$translation->for_translation($this->project, $this, 'no-limit', array('status' => 'current', 'translated' => 'yes'));
     $current_translations = new Translations();
     foreach ($current_translations_list as $entry) {
         $current_translations->add_entry($entry);
     }
     unset($current_translations_list);
     $translations_added = 0;
     foreach ($translations->entries as $entry) {
         if (empty($entry->translations)) {
             continue;
         }
         if (in_array('fuzzy', $entry->flags)) {
             continue;
         }
         $create = false;
         if ($translated = $current_translations->translate_entry($entry)) {
             // we have the same string translated
             // create a new one if they don't match
             $entry->original_id = $translated->original_id;
             $create = array_pad($entry->translations, $locale->nplurals, null) != $translated->translations;
         } else {
             // we don't have the string translated, let's see if the original is there
             $original = GP::$original->by_project_id_and_entry($this->project->id, $entry);
             if ($original) {
                 $entry->original_id = $original->id;
                 $create = true;
             }
         }
         if ($create) {
             $entry->translation_set_id = $this->id;
             $entry->status = 'current';
             // check for errors
             $translation = GP::$translation->create($entry);
             $translation->set_as_current();
             $translations_added += 1;
         }
     }
     wp_cache_delete($this->id, 'translation_set_status_breakdown');
     return $translations_added;
 }
 /**
  * @ticket 512
  */
 function test_filter_translation_set_import_fuzzy_translations()
 {
     $set = $this->factory->translation_set->create_with_project_and_locale();
     $translations_for_import = new Translations();
     // Create 3 originals and 3 fuzzy translations
     for ($i = 0; $i < 3; $i++) {
         $this->factory->original->create(array('project_id' => $set->project->id, 'status' => '+active', 'singular' => "A string #{$i}"));
         $translations_for_import->add_entry(array('singular' => "A string #{$i}", 'translations' => array("A translated string #{$i}"), 'flags' => array('fuzzy')));
     }
     // Create 3 originals and 3 non-fuzzy translations
     for ($i = 0; $i < 3; $i++) {
         $this->factory->original->create(array('project_id' => $set->project->id, 'status' => '+active', 'singular' => "A second string #{$i}"));
         $translations_for_import->add_entry(array('singular' => "A second string #{$i}", 'translations' => array("A second translated string #{$i}")));
     }
     // Import 6 translations
     add_filter('gp_translation_set_import_fuzzy_translations', '__return_false');
     $translations_added = $set->import($translations_for_import);
     remove_filter('gp_translation_set_import_fuzzy_translations', '__return_false');
     // Expect only 3 imported translations, fuzzy translations are ignored.
     $this->assertEquals($translations_added, 3);
 }
Beispiel #12
0
 public function read_originals_from_file($file_name)
 {
     $errors = libxml_use_internal_errors(true);
     $data = simplexml_load_string(file_get_contents($file_name));
     libxml_use_internal_errors($errors);
     if (!is_object($data)) {
         return false;
     }
     $entries = new Translations();
     foreach ($data->data as $string) {
         $entry = new Translation_Entry();
         if (isset($string['type']) && gp_in('System.Resources.ResXFileRef', (string) $string['type'])) {
             continue;
         }
         $entry->context = (string) $string['name'];
         $entry->singular = $this->unescape((string) $string->value);
         if (isset($string->comment) && $string->comment) {
             $entry->extracted_comments = (string) $string->comment;
         }
         $entry->translations = array();
         $entries->add_entry($entry);
     }
     return $entries;
 }
 public function read_originals_from_file($file_name)
 {
     $entries = new Translations();
     $file = file_get_contents($file_name);
     if (false === $file) {
         return false;
     }
     $file = mb_convert_encoding($file, 'UTF-8', 'UTF-16LE');
     $context = $comment = null;
     $lines = explode("\n", $file);
     foreach ($lines as $line) {
         if (is_null($context)) {
             if (preg_match('/^\\/\\*\\s*(.*)\\s*\\*\\/$/', $line, $matches)) {
                 $matches[1] = trim($matches[1]);
                 if ($matches[1] !== "No comment provided by engineer.") {
                     $comment = $matches[1];
                 } else {
                     $comment = null;
                 }
             } else {
                 if (preg_match('/^"(.*)" = "(.*)";$/', $line, $matches)) {
                     $entry = new Translation_Entry();
                     $entry->context = $this->unescape($matches[1]);
                     $entry->singular = $this->unescape($matches[2]);
                     if (!is_null($comment)) {
                         $entry->extracted_comments = $comment;
                         $comment = null;
                     }
                     $entry->translations = array();
                     $entries->add_entry($entry);
                 }
             }
         }
     }
     return $entries;
 }
 public function import($translations)
 {
     $this->set_memory_limit('256M');
     if (!isset($this->project) || !$this->project) {
         $this->project = GP::$project->get($this->project_id);
     }
     $locale = GP_Locales::by_slug($this->locale);
     $user = wp_get_current_user();
     $current_translations_list = GP::$translation->for_translation($this->project, $this, 'no-limit', array('status' => 'current', 'translated' => 'yes'));
     $current_translations = new Translations();
     foreach ($current_translations_list as $entry) {
         $current_translations->add_entry($entry);
     }
     unset($current_translations_list);
     $translations_added = 0;
     foreach ($translations->entries as $entry) {
         if (empty($entry->translations)) {
             continue;
         }
         $is_fuzzy = in_array('fuzzy', $entry->flags);
         /**
          * Filter whether to import fuzzy translations.
          *
          * @since 1.0.0
          *
          * @param bool              $import_over  Import fuzzy translation. Default true.
          * @param Translation_Entry $entry        Translation entry object to import.
          * @param Translations      $translations Translations collection.
          */
         if ($is_fuzzy && !apply_filters('gp_translation_set_import_fuzzy_translations', true, $entry, $translations)) {
             continue;
         }
         $create = false;
         if ($translated = $current_translations->translate_entry($entry)) {
             // we have the same string translated
             // create a new one if they don't match
             $entry->original_id = $translated->original_id;
             $translated_is_different = array_pad($entry->translations, $locale->nplurals, null) != $translated->translations;
             /**
              * Filter whether to import over an existing translation on a translation set.
              *
              * @since 1.0.0
              *
              * @param bool $import_over Import over an existing translation.
              */
             $create = apply_filters('gp_translation_set_import_over_existing', $translated_is_different);
         } else {
             // we don't have the string translated, let's see if the original is there
             $original = GP::$original->by_project_id_and_entry($this->project->id, $entry, '+active');
             if ($original) {
                 $entry->original_id = $original->id;
                 $create = true;
             }
         }
         if ($create) {
             if ($user) {
                 $entry->user_id = $user->ID;
             }
             $entry->translation_set_id = $this->id;
             /**
              * Filter the the status of imported translations of a translation set.
              *
              * @since 1.0.0
              *
              * @param string $status The status of imported translations.
              */
             $entry->status = apply_filters('gp_translation_set_import_status', $is_fuzzy ? 'fuzzy' : 'current');
             // check for errors
             $translation = GP::$translation->create($entry);
             $translation->set_status($entry->status);
             $translations_added += 1;
         }
     }
     gp_clean_translation_set_cache($this->id);
     /**
      * Fires after translations have been imported to a translation set.
      *
      * @since 1.0.0
      *
      * @param int $translation_set The ID of the translation set the import was made into.
      */
     do_action('gp_translations_imported', $this->id);
     return $translations_added;
 }
 /**
  * Reads a set of original strings from a properties file.
  *
  * @since 2.0.0
  *
  * @param string $file_name The filename of the uploaded properties file.
  *
  * @return Translations|bool
  */
 public function read_originals_from_file($file_name)
 {
     $entries = new Translations();
     $file = file_get_contents($file_name);
     if (false === $file) {
         return false;
     }
     $entry = $comment = null;
     $inline = false;
     $lines = explode("\n", $file);
     $key = '';
     $value = '';
     foreach ($lines as $line) {
         if (preg_match('/^(#|!)\\s*(.*)\\s*$/', $line, $matches)) {
             // If we have been processing a multi-line entry, save it now.
             if (true === $inline) {
                 $entries->add_entry($entry);
                 $inline = false;
             }
             $matches[1] = trim($matches[1]);
             if ($matches[1] !== "No comment provided.") {
                 if (null !== $comment) {
                     $comment = $comment . "\n" . $matches[2];
                 } else {
                     $comment = $matches[2];
                 }
             } else {
                 $comment = null;
             }
         } else {
             if (false === $inline && $this->split_properties_line($line, $key, $value)) {
                 // Check to see if this line continues on to the next
                 if (gp_endswith($line, '\\')) {
                     $inline = true;
                     $value = trim($value, '\\');
                 }
                 $entry = new Translation_Entry();
                 $entry->context = rtrim($this->unescape($key));
                 /* So the following line looks a little weird, why encode just to decode?
                  *
                  * The reason is simple, properties files are in ISO-8859-1 aka Latin-1 format
                  * and can have extended characters above 127 but below 256 represented by a
                  * single byte.  That will break things later as PHP/MySQL will not accept
                  * a mixed encoding string with these high single byte characters in them.
                  *
                  * So let's convert everything to escaped unicode first and then decode
                  * the whole kit and kaboodle to UTF-8.
                  */
                 $entry->singular = $this->uni_decode($this->ascii_uni_encode($value));
                 if (!is_null($comment)) {
                     $entry->extracted_comments = $comment;
                     $comment = null;
                 }
                 $entry->translations = array();
                 // Only save this entry if we're not in a multi line translation.
                 if (false === $inline) {
                     $entries->add_entry($entry);
                 }
             } else {
                 // If we're processing a multi-line entry, add the line to the translation.
                 if (true === $inline) {
                     // Check to make sure we're not a blank line.
                     if ('' != trim($line)) {
                         // If there's still more lines to add, trim off the trailing slash.
                         if (gp_endswith($line, '\\')) {
                             $line = rtrim($line, '\\');
                         }
                         // Strip off leading spaces.
                         $line = ltrim($line);
                         // Decode the translation and add it to the current entry.
                         $entry->singular = $entry->singular . $this->uni_decode($line);
                     } else {
                         // Any blank line signals end of the entry.
                         $entries->add_entry($entry);
                         $inline = false;
                     }
                 } else {
                     // If we hit a blank line and are not processing a multi-line entry, reset the comment.
                     $comment = null;
                 }
             }
         }
     }
     // Make sure we save the last entry if it is a multi-line entry.
     if (true === $inline) {
         $entries->add_entry($entry);
     }
     return $entries;
 }