Example #1
0
 /**
  * Cleanup the custom translations for one plugin
  *
  * @param string $language  the language to cleanup for
  * @param string $plugin_id the plugin to cleanup
  *
  * @return bool
  */
 protected static function cleanupPlugin($language, $plugin_id)
 {
     if (empty($language) || empty($plugin_id)) {
         return false;
     }
     $translation = translation_editor_get_plugin($language, $plugin_id);
     if ($translation === false) {
         return false;
     }
     $custom = elgg_extract('custom', $translation);
     if (empty($custom)) {
         // no custom translation, how did you get here??
         return true;
     }
     $english = elgg_extract('en', $translation);
     $clean_custom = array_intersect_key($custom, $english);
     $removed_custom = array_diff_key($custom, $clean_custom);
     // report cleaned-up translations
     if (empty($removed_custom)) {
         // nothing removed
         return true;
     } else {
         self::writeCleanupTranslations($language, $plugin_id, $removed_custom);
     }
     // write new custom translation
     if (empty($clean_custom)) {
         // no more custom translations
         translation_editor_delete_translation($language, $plugin_id);
     } else {
         // write new custom translation
         translation_editor_write_translation($language, $plugin_id, $clean_custom);
     }
     return true;
 }
Example #2
0
function translation_editor_search_translation($query, $language = "en")
{
    $result = false;
    $plugins = translation_editor_get_plugins($language);
    $found = array();
    foreach ($plugins as $plugin => $data) {
        if ($translations = translation_editor_get_plugin($language, $plugin)) {
            foreach ($translations["en"] as $key => $value) {
                if (stristr($key, $query) || stristr($value, $query) || array_key_exists($key, $translations["current_language"]) && stristr($translations["current_language"][$key], $query)) {
                    if (!array_key_exists($plugin, $found)) {
                        $found[$plugin] = array("en" => array(), "current_language" => array());
                    }
                    $found[$plugin]["en"][$key] = $value;
                    if (array_key_exists($key, $translations["current_language"])) {
                        $found[$plugin]["current_language"][$key] = $translations["current_language"][$key];
                    }
                }
            }
            if (!empty($found)) {
                $result = $found;
            }
        }
    }
    return $result;
}
    forward(REFERER);
}
if (is_numeric($key)) {
    register_error(elgg_echo('translation_editor:action:add_custom_key:key_numeric'));
    forward(REFERER);
}
if (!preg_match('/^[a-zA-Z0-9_:]{1,}$/', $key)) {
    register_error(elgg_echo('translation_editor:action:add_custom_key:invalid_chars'));
    forward(REFERER);
}
if (elgg_language_key_exists($key, 'en')) {
    register_error(elgg_echo('translation_editor:action:add_custom_key:exists'));
    forward(REFERER);
}
// save
$custom_translations = translation_editor_get_plugin('en', 'custom_keys');
if (!empty($custom_translations)) {
    $custom_translations = $custom_translations['en'];
} else {
    $custom_translations = array();
}
$custom_translations[$key] = $translation;
$base_dir = elgg_get_data_path() . 'translation_editor' . DIRECTORY_SEPARATOR;
if (!file_exists($base_dir)) {
    mkdir($base_dir, 0755, true);
}
$location = $base_dir . 'custom_keys' . DIRECTORY_SEPARATOR;
if (!file_exists($location)) {
    mkdir($location, 0755, true);
}
$file_contents = '<?php' . PHP_EOL;
Example #4
0
}
$plugin = get_input("plugin");
$languages = array_keys($CONFIG->translations);
if (!($disabled_languages = translation_editor_get_disabled_languages())) {
    $disabled_languages = array();
}
if (!empty($CONFIG->language)) {
    $site_language = $CONFIG->language;
} else {
    $site_language = "en";
}
$body .= elgg_view("translation_editor/language_selector", array("current_language" => $current_language, "plugin" => $plugin, "languages" => $languages, "disabled_languages" => $disabled_languages, "site_language" => $site_language));
if (empty($plugin)) {
    // show plugin list
    elgg_push_breadcrumb(elgg_echo($current_language));
    $plugins = translation_editor_get_plugins($current_language);
    $body .= elgg_view("translation_editor/search", array("current_language" => $current_language, "query" => get_input("q")));
    $body .= elgg_view("translation_editor/plugin_list", array("plugins" => $plugins, "current_language" => $current_language));
} else {
    // show plugin keys
    elgg_push_breadcrumb(elgg_echo($current_language), "translation_editor/" . $current_language);
    elgg_push_breadcrumb($plugin);
    $translation = translation_editor_get_plugin($current_language, $plugin);
    if ($plugin == "custom_keys" && elgg_is_admin_logged_in()) {
        $body .= elgg_view("translation_editor/add_custom_key");
    }
    $body .= elgg_view("translation_editor/plugin_edit", array("plugin" => $plugin, "current_language" => $current_language, "translation" => $translation));
}
// Build page
$page_data = elgg_view_layout('one_column', array('title' => $title_text, 'content' => $body));
echo elgg_view_page($title_text, $page_data);
global $CONFIG;
admin_gatekeeper();
$key = get_input("key");
$translation = get_input("translation");
if (!empty($key) && !empty($translation)) {
    if (!is_numeric($key)) {
        if (preg_match("/^[a-zA-Z0-9_:]{1,}\$/", $key)) {
            $exists = false;
            if (array_key_exists($key, $CONFIG->translations["en"])) {
                $exists = true;
            }
            if (!$exists) {
                // save
                $custom_translations = array();
                if ($custom_translations = translation_editor_get_plugin("en", "custom_keys")) {
                    $custom_translations = $custom_translations["en"];
                }
                $custom_translations[$key] = $translation;
                $base_dir = elgg_get_data_path() . "translation_editor" . DIRECTORY_SEPARATOR;
                if (!file_exists($base_dir)) {
                    mkdir($base_dir);
                }
                $location = $base_dir . "custom_keys" . DIRECTORY_SEPARATOR;
                if (!file_exists($location)) {
                    mkdir($location);
                }
                $file_contents = "<?php" . PHP_EOL;
                $file_contents .= '$language = ';
                $file_contents .= var_export($custom_translations, true);
                $file_contents .= ';' . PHP_EOL;
    forward(REFERER);
}
$trans = get_installed_translations();
foreach ($translation as $language => $plugins) {
    if (!array_key_exists($language, $trans)) {
        continue;
    }
    if (!is_array($plugins)) {
        continue;
    }
    foreach ($plugins as $plugin_name => $translate_input) {
        if (!is_array($translate_input)) {
            continue;
        }
        // get plugin translation
        $plugin_translation = translation_editor_get_plugin($language, $plugin_name);
        // merge with existing custom translations
        $custom_translation = elgg_extract('custom', $plugin_translation);
        if (!empty($custom_translation)) {
            $translate_input = array_merge($custom_translation, $translate_input);
        }
        // get original plugin keys
        $original_keys = elgg_extract('en', $plugin_translation);
        // only keep keys which are present in the plugin
        $translate_input = array_intersect_key($translate_input, $original_keys);
        // check if translated
        $translated = translation_editor_compare_translations($language, $translate_input);
        if (!empty($translated)) {
            if (translation_editor_write_translation($language, $plugin_name, $translated)) {
                system_message(elgg_echo('translation_editor:action:translate:success'));
            } else {
/**
 * Search for a translation
 *
 * @param string $query    the text to search for
 * @param string $language the language to search in (defaults to English)
 *
 * @return array|bool
 */
function translation_editor_search_translation($query, $language = 'en')
{
    $plugins = translation_editor_get_plugins($language);
    if (empty($plugins)) {
        return false;
    }
    $found = [];
    foreach ($plugins as $plugin => $data) {
        $translations = translation_editor_get_plugin($language, $plugin);
        if (empty($translations) || empty(elgg_extract('total', $translations))) {
            continue;
        }
        foreach ($translations['en'] as $key => $value) {
            if (stristr($key, $query) || stristr($value, $query) || array_key_exists($key, $translations['current_language']) && stristr($translations['current_language'][$key], $query)) {
                if (!array_key_exists($plugin, $found)) {
                    $found[$plugin] = ['en' => [], 'current_language' => []];
                }
                $found[$plugin]['en'][$key] = $value;
                if (array_key_exists($key, $translations['current_language'])) {
                    $found[$plugin]['current_language'][$key] = $translations['current_language'][$key];
                }
            }
        }
    }
    if (empty($found)) {
        return false;
    }
    return $found;
}