function add_entry($entry) { if (is_array($entry)) { $entry = new Translation_Entry($entry); } $key = $entry->key(); if (false === $key) return false; $this->entries[$key] = &$entry; return true; }
function test_key() { $entry_baba = new Translation_Entry(array('singular' => 'baba')); $entry_dyado = new Translation_Entry(array('singular' => 'dyado')); $entry_baba_ctxt = new Translation_Entry(array('singular' => 'baba', 'context' => 'x')); $entry_baba_plural = new Translation_Entry(array('singular' => 'baba', 'plural' => 'babas')); $this->assertEquals($entry_baba->key(), $entry_baba_plural->key()); $this->assertNotEquals($entry_baba->key(), $entry_baba_ctxt->key()); $this->assertNotEquals($entry_baba_plural->key(), $entry_baba_ctxt->key()); $this->assertNotEquals($entry_baba->key(), $entry_dyado->key()); }
function add_entry_or_merge($entry) { if (is_array($entry)) { $entry = new Translation_Entry($entry); } $key = $entry->key(); if (false === $key) { return false; } if (isset($this->entries[$key])) { $this->entries[$key]->merge_with($entry); } else { $this->entries[$key] =& $entry; } return true; }
function import_for_project($project, $translations) { global $gpdb; $originals_added = $originals_existing = $originals_obsoleted = $originals_fuzzied = 0; $all_originals_for_project = $this->many_no_map("SELECT * FROM {$this->table} WHERE project_id= %d", $project->id); $originals_by_key = array(); foreach ($all_originals_for_project as $original) { $entry = new Translation_Entry(array('singular' => $original->singular, 'plural' => $original->plural, 'context' => $original->context)); $originals_by_key[$entry->key()] = $original; } $obsolete_originals = array_filter($originals_by_key, function ($entry) { return '-obsolete' == $entry->status; }); $possibly_added = $possibly_dropped = array(); foreach ($translations->entries as $entry) { $gpdb->queries = array(); $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 'references' => implode(' ', $entry->references), 'status' => '+active'); $data = apply_filters('import_original_array', $data); // Original exists, let's update it. if (isset($originals_by_key[$entry->key()])) { $original = $originals_by_key[$entry->key()]; if ($original->status == '-obsolete' || GP::$original->is_different_from($data, $original)) { $this->update($data, array('id' => $original->id)); } $originals_existing++; } else { // We can't find this in our originals. Let's keep it for later. $possibly_added[] = $entry; } } // Mark missing strings as possible removals. foreach ($originals_by_key as $key => $value) { if ($value->status != '-obsolete' && is_array($translations->entries) && !array_key_exists($key, $translations->entries)) { $possibly_dropped[$key] = $value; } } $comparison_array = array_unique(array_merge(array_keys($possibly_dropped), array_keys($obsolete_originals))); foreach ($possibly_added as $entry) { $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 'references' => implode(' ', $entry->references), 'status' => '+active'); $data = apply_filters('import_original_array', $data); // Search for match in the dropped strings and existing obsolete strings. $close_original = $this->closest_original($entry->key(), $comparison_array); // We found a match - probably a slightly changed string. if ($close_original) { $original = $originals_by_key[$close_original]; // We'll update the old original... $this->update($data, array('id' => $original->id)); // and set existing translations to fuzzy. $this->set_translations_for_original_to_fuzzy($original->id); // No need to obsolete it now. unset($possibly_dropped[$close_original]); $originals_fuzzied++; continue; } else { // Completely new string $created = GP::$original->create($data); if (apply_filters('enable_add_translations_from_other_projects', true)) { $created->add_translations_from_other_projects(); } $originals_added++; } } // Mark remaining possibly dropped strings as obsolete. foreach ($possibly_dropped as $key => $value) { $this->update(array('status' => '-obsolete'), array('id' => $value->id)); $originals_obsoleted++; } // Clear cache when the amount of strings are changed. if ($originals_added > 0 || $originals_fuzzied > 0 || $originals_obsoleted > 0) { wp_cache_delete($project->id, self::$count_cache_group); } do_action('originals_imported', $project->id, $originals_added, $originals_existing, $originals_obsoleted, $originals_fuzzied); return array($originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted); }
/** * @param Translation_Entry $entry */ function translate_entry(&$entry) { $key = $entry->key(); return isset($this->entries[$key]) ? $this->entries[$key] : false; }
/** * @param Translation_Entry $entry */ function translate_entry(&$entry) { $key = $entry->key(); $key = str_replace("\r\n", "\n", $key); return isset($this->entries[$key]) ? $this->entries[$key] : false; }
public function import_for_project($project, $translations) { global $wpdb; $originals_added = $originals_existing = $originals_obsoleted = $originals_fuzzied = $originals_error = 0; $all_originals_for_project = $this->many_no_map("SELECT * FROM {$this->table} WHERE project_id= %d", $project->id); $originals_by_key = array(); foreach ($all_originals_for_project as $original) { $entry = new Translation_Entry(array('singular' => $original->singular, 'plural' => $original->plural, 'context' => $original->context)); $originals_by_key[$entry->key()] = $original; } $obsolete_originals = array_filter($originals_by_key, function ($entry) { return '-obsolete' == $entry->status; }); $possibly_added = $possibly_dropped = array(); foreach ($translations->entries as $key => $entry) { $wpdb->queries = array(); // Context needs to match VARCHAR(255) in the database schema. if (gp_strlen($entry->context) > 255) { $entry->context = gp_substr($entry->context, 0, 255); $translations->entries[$entry->key()] = $entry; } $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 'references' => implode(' ', $entry->references), 'status' => '+active'); /** * Filter the data of an original being imported or updated. * * This filter is called twice per each entry. First time during determining if the original * already exists. The second time it is called before a new original is added or a close * old match is set fuzzy with this new data. * * @since 1.0.0 * * @param array $data { * An array that describes a single entry being imported or updated. * * @type string $project_id Project id to import into. * @type string $context Context information. * @type string $singular Translation string of the singular form. * @type string $plural Translation string of the plural form. * @type string $comment Comment for translators. * @type string $references Referenced in code. A single reference is represented by a file * path followed by a colon and a line number. Multiple references * are separated by spaces. * @type string $status Status of the imported original. * } */ $data = apply_filters('gp_import_original_array', $data); // Original exists, let's update it. if (isset($originals_by_key[$entry->key()])) { $original = $originals_by_key[$entry->key()]; // But only if it's different, like a changed 'references', 'comment', or 'status' field. if (GP::$original->is_different_from($data, $original)) { $this->update($data, array('id' => $original->id)); $originals_existing++; } } else { // We can't find this in our originals. Let's keep it for later. $possibly_added[] = $entry; } } // Mark missing strings as possible removals. foreach ($originals_by_key as $key => $value) { if ($value->status != '-obsolete' && is_array($translations->entries) && !array_key_exists($key, $translations->entries)) { $possibly_dropped[$key] = $value; } } $comparison_array = array_unique(array_merge(array_keys($possibly_dropped), array_keys($obsolete_originals))); $prev_suspend_cache = wp_suspend_cache_invalidation(true); foreach ($possibly_added as $entry) { $data = array('project_id' => $project->id, 'context' => $entry->context, 'singular' => $entry->singular, 'plural' => $entry->plural, 'comment' => $entry->extracted_comments, 'references' => implode(' ', $entry->references), 'status' => '+active'); /** This filter is documented in gp-includes/things/original.php */ $data = apply_filters('gp_import_original_array', $data); // Search for match in the dropped strings and existing obsolete strings. $close_original = $this->closest_original($entry->key(), $comparison_array); // We found a match - probably a slightly changed string. if ($close_original) { $original = $originals_by_key[$close_original]; // We'll update the old original... $this->update($data, array('id' => $original->id)); // and set existing translations to fuzzy. $this->set_translations_for_original_to_fuzzy($original->id); // No need to obsolete it now. unset($possibly_dropped[$close_original]); $originals_fuzzied++; continue; } else { // Completely new string $created = GP::$original->create($data); if (!$created) { $originals_error++; continue; } $originals_added++; } } // Mark remaining possibly dropped strings as obsolete. foreach ($possibly_dropped as $key => $value) { $this->update(array('status' => '-obsolete'), array('id' => $value->id)); $originals_obsoleted++; } wp_suspend_cache_invalidation($prev_suspend_cache); // Clear cache when the amount of strings are changed. if ($originals_added > 0 || $originals_existing > 0 || $originals_fuzzied > 0 || $originals_obsoleted > 0) { wp_cache_delete($project->id, self::$count_cache_group); gp_clean_translation_sets_cache($project->id); } /** * Fires after originals have been imported. * * @since 1.0.0 * * @param string $project_id Project ID the import was made to. * @param int $originals_added Number or total originals added. * @param int $originals_existing Number of existing originals updated. * @param int $originals_obsoleted Number of originals that were marked as obsolete. * @param int $originals_fuzzied Number of originals that were close matches of old ones and thus marked as fuzzy. * @param int $originals_error Number of originals that were not imported due to an error. */ do_action('gp_originals_imported', $project->id, $originals_added, $originals_existing, $originals_obsoleted, $originals_fuzzied, $originals_error); return array($originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error); }