Пример #1
0
 /**
  * gets the translation as array form a language
  * @param  string $language 
  * @return array           
  */
 public function get_translation($language)
 {
     $mo_translation = i18n::get_language_path($language);
     if (!file_exists($mo_translation)) {
         Alert::set(Alert::ERROR, $language);
         $this->redirect(Route::url('oc-panel', array('controller' => 'translations')));
     }
     $base_translation = i18n::get_language_path();
     //pear gettext scripts
     require_once Kohana::find_file('vendor', 'GT/Gettext', 'php');
     require_once Kohana::find_file('vendor', 'GT/Gettext/PO', 'php');
     require_once Kohana::find_file('vendor', 'GT/Gettext/MO', 'php');
     //load the .po files
     //original en translation
     $pocreator_en = new File_Gettext_PO();
     $pocreator_en->load($base_translation);
     //the translation file
     $pocreator_translated = new File_Gettext_PO();
     $pocreator_translated->load($mo_translation);
     //get an array with all the strings
     $en_array_order = $pocreator_en->strings;
     //sort alphabetical using locale
     ksort($en_array_order, SORT_LOCALE_STRING);
     //array with translated language may contain missing from EN
     $origin_translation = $pocreator_translated->strings;
     //lets get the array with translated values and sorted, will include everything even if was not previously saved
     $translation_array = array();
     $untranslated_array = array();
     //keep track of words not translated stores ID
     $i = 0;
     foreach ($en_array_order as $origin => $value) {
         //do we have the translation?
         if (isset($origin_translation[$origin]) and !empty($origin_translation[$origin]) > 0) {
             $translated = $origin_translation[$origin];
         } else {
             $untranslated_array[] = $i;
             $translated = '';
         }
         $translation_array[] = array('id' => $i, 'original' => $origin, 'translated' => $translated);
         $i++;
     }
     return array($translation_array, $untranslated_array);
 }
Пример #2
0
 public function action_edit()
 {
     if ($this->request->param('id')) {
         $language = $this->request->param('id');
         $default = DOCROOT . 'languages/' . $language . '/LC_MESSAGES/messages.po';
         $default_mo = DOCROOT . 'languages/' . $language . '/LC_MESSAGES/messages.mo';
     } else {
         Request::current()->redirect(Route::url('oc-panel', array('controller' => 'translations')));
     }
     Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Edit Translation')));
     $this->template->title = __('Edit Translation');
     $this->template->bind('content', $content);
     $content = View::factory('oc-panel/pages/translations/edit');
     $this->template->scripts['footer'][] = 'js/oc-panel/translations.js';
     $base_translation = i18n::get_language_path();
     //pear gettext scripts
     require_once Kohana::find_file('vendor', 'GT/Gettext', 'php');
     require_once Kohana::find_file('vendor', 'GT/Gettext/PO', 'php');
     require_once Kohana::find_file('vendor', 'GT/Gettext/MO', 'php');
     //.po to .mo script
     require_once Kohana::find_file('vendor', 'php-mo/php-mo', 'php');
     //load the .po files
     $pocreator_en = new File_Gettext_PO();
     $pocreator_en->load($base_translation);
     $pocreator_default = new File_Gettext_PO();
     $pocreator_default->load($default);
     $en_array_order = $pocreator_en->strings;
     //        ksort($en_array_order,SORT_NATURAL); // since PHP 5.4
     natcasesort($en_array_order);
     // better than natsort() ??
     //watch out there's a limit of 1000 posts....we have 540...
     if ($this->request->post()) {
         foreach ($en_array_order as $key => $string) {
             $keys[] = $key;
         }
         $translations = $this->request->post('translations');
         $strings = array();
         $out = '';
         foreach ($translations as $key => $value) {
             if ($value != "") {
                 $strings[$keys[$key]] = $value;
                 $out .= '#: String ' . $key . PHP_EOL;
                 $out .= 'msgid "' . $keys[$key] . '"' . PHP_EOL;
                 $out .= 'msgstr "' . $value . '"' . PHP_EOL;
                 $out .= PHP_EOL;
             }
         }
         //write the generated .po to file
         $fp = fopen($default, 'w+');
         $read = fwrite($fp, $out);
         fclose($fp);
         $pocreator_default->strings = $strings;
         //generate the .mo from the .po file
         phpmo_convert($default);
         Alert::set(Alert::SUCCESS, $this->request->param('id') . ' ' . __('Language saved'));
         Request::current()->redirect(Route::url('oc-panel', array('controller' => 'translations', 'action' => 'edit', 'id' => $this->request->param('id'))));
     }
     $content->edit_language = $this->request->param('id');
     $content->strings_en = $en_array_order;
     $content->strings_default = $pocreator_default->strings;
 }