/**
  * Imports a file with translations into the translation system.
  * 
  * File must contain vaid JSON data with key-value pairs of strings in an array.
  * Sample:
  * <code json>
  * [
  *   {"term":"TXT_TERM1","content":"My contents of terms 1"},
  *   {"or":"TXT_TERM2","can_be":"My contents of terms 2"},
  *   {"whatever":"TXT_TERM3","wtf_too":"My contents of terms 3"}
  * ]
  * </code>
  * Notes:
  * - The key names will be ignored, only the order counts
  * - The contents must be in the language defined by the `$lang` parameter
  * - The uploaded file must be given as 'json_file' (`$_FILES['json_file']`)
  * @param string $lang The language the contents are given in
  * @return void
  * @internal
  * @attribute[RequestParam('lang','string',false)]
  */
 function Import($lang)
 {
     global $CONFIG;
     $lang = $lang ? $lang : $CONFIG['localization']['default_language'];
     $is_default = $lang == $CONFIG['localization']['default_language'];
     if (isset($_FILES['json_file'])) {
         $json_string = file_get_contents($_FILES['json_file']['tmp_name']);
         unlink($_FILES['json_file']['tmp_name']);
         $count = 0;
         $unknowns = "";
         if (!$is_default) {
             $knowns = $this->ds->ExecuteSql("SELECT DISTINCT id FROM wdf_translations WHERE lang=?", $CONFIG['localization']['default_language'])->Enumerate('id', false);
         }
         foreach (json_decode($json_string, true) as $entry) {
             $entry = array_values($entry);
             if (count($entry) < 2 || !$entry[1]) {
                 continue;
             }
             $this->ds->ExecuteSql("REPLACE INTO wdf_translations(lang,id,content)VALUES(?,?,?)", array($lang, $entry[0], $entry[1]));
             $count++;
             if (!$is_default && !in_array($entry[0], $knowns)) {
                 $unknowns[] = $entry[0];
                 default_string($entry[0], "Imported from {$lang}: " . $entry[1]);
             }
         }
         if ($is_default) {
             $this->ds->ExecuteSql("DELETE FROM wdf_unknown_strings WHERE term IN(SELECT id FROM wdf_translations WHERE lang=?)", $lang);
         } else {
             log_debug($unknowns);
             translation_add_unknown_strings($unknowns);
         }
         $this->content("<h2>{$count} terms imported</h2>");
     }
     $form = $this->content(new Form());
     $form->content($this->_languageSelect($lang))->name = 'lang';
     $form->AddFile('json_file');
     $form->AddSubmit('Import');
 }
Exemple #2
0
function __translate($text)
{
    global $CONFIG, $__unknown_constants;
    if (!is_string($text)) {
        return $text;
    }
    $text = preg_replace_callback($GLOBALS['__translate_regpattern'], '__translate_callback', $text);
    if (ends_with($text, '[NT]')) {
        $text = substr($text, 0, -4);
    }
    if (count($__unknown_constants) > 0) {
        translation_add_unknown_strings($__unknown_constants);
    }
    return $text;
}