public static function translation_gui() { require_once get_kigo_plugin_path(self::INCLUDE_PATH . DIRECTORY_SEPARATOR . 'class-kigo-translations-list-table.php'); $my_table = new Kigo_Translations_List_Table(); if (!$my_table->prepare_items()) { wp_die('Sorry, we were unable to retrieve the translations'); } include get_kigo_plugin_path(self::ADMIN_VIEW_PATH . DIRECTORY_SEPARATOR . 'setup-translations.php'); }
/** * Update (generate if not exist) the language file by google translating the missing keys in comparison to the english version * * @param string $lang_code * @param array $translations_keys * * @return bool */ private static function update_lang_file($lang_code, $default_lang_translations) { $file_path = get_kigo_plugin_path(self::I18N_FOLDER_NAME . DIRECTORY_SEPARATOR . $lang_code . '.' . self::I18N_FILE_EXTENSION); if (!is_array($default_lang_translations) || !is_string($lang_code)) { return false; } if (!file_exists($file_path) || !is_string($file_content = file_get_contents($file_path)) || !strlen($file_content) || !is_array($existing_trad = self::parse_i18n_file($file_content))) { $existing_trad = array(); } // Retrieve the list of needed translations, we use the english values (not key) to translate $needed_sentences = array_diff_key($default_lang_translations, $existing_trad); // Check if there are no translations in the file that has been deleted $content_to_writte = array_intersect_key($existing_trad, $default_lang_translations); // Check if there is any reason to cleanup and rewrite the file if (!count($needed_sentences) && count($content_to_writte) === count($existing_trad)) { return true; } // Do the call to google translate by chunks of 50 sentences, because google has an undocumented limit. $needed_sentences_chunks = array_chunk($needed_sentences, 50, true); foreach ($needed_sentences_chunks as $needed_sentences_chunk) { // Retrieve translations needed from google translate API if (!is_string($translations_json = file_get_contents(self::build_google_translate_url($lang_code, array_values($needed_sentences_chunk)))) || !is_array($translations = json_decode($translations_json, true)) || !isset($translations['data']) || !isset($translations['data']['translations']) || !is_array($translations['data']['translations']) || count($translations['data']['translations']) !== count($needed_sentences_chunk)) { return false; } // Build the translation array $needed_sentences_chunk_keys = array_keys($needed_sentences_chunk); foreach ($translations['data']['translations'] as $idx => $translated_sentence) { if (!isset($needed_sentences_chunk_keys[$idx]) || !strlen($needed_sentences_chunk_keys[$idx]) || !isset($translated_sentence['translatedText']) || !strlen($translated_sentence['translatedText'])) { continue; } $content_to_writte[$needed_sentences_chunk_keys[$idx]] = $translated_sentence['translatedText']; } } if (!count($content_to_writte) || !array_walk($content_to_writte, function (&$value, $key) { $value = $key . kigo_I18n::I18N_FILE_SEPARATOR . $value; }) || !is_resource($resource = fopen($file_path, 'w')) || !is_int(fwrite($resource, implode(self::I18N_FILE_LINE_SEPARATOR, $content_to_writte)))) { fclose($resource); return false; } return fclose($resource); }