From e135975655eb7be56e4349724f93ec93eb1cc33d Mon Sep 17 00:00:00 2001 From: filip Date: Wed, 5 Oct 2016 21:18:40 +0200 Subject: added parsing of ts strings in array --- langs/lib.php | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'langs/lib.php') diff --git a/langs/lib.php b/langs/lib.php index 263088142..6dc27c26d 100644 --- a/langs/lib.php +++ b/langs/lib.php @@ -224,31 +224,57 @@ function _ts_diff($locale, $resource, $source_l = NULL, $path = NULL, $compared } /** - * Parse ts files, to get: + * Parse ts files or strings in array, to get: * - source (ts) strings count * - source strings * - untranslated strings * - obsoleted strings * - * @param string $ts_path_filename full path and filename to ts file + * @param string $ts_path_filename_or_content full path and filename of ts file or strings in array in that format * * @return array of $num_of_original_str, $source_strings, $untranslated_strings and $obsoleted_strings */ -function _parse_ts_file($ts_path_filename) +function _parse_ts_file($ts_path_filename_or_content, $translation_in_file = true) { $source_strings = array(); $untranslated_strings = array(); $obsoleted_strings = array(); $num_of_original_str = 0; - // read .ts file - $file_handle = @fopen($ts_path_filename, 'r'); - if ($file_handle === false) { - // Could not open file resource - return false; + $parse_condition = true; + + if ($translation_in_file === true) { + // read .ts file + $file_handle = @fopen($ts_path_filename_or_content, 'r'); + if ($file_handle === false) { + // Could not open file resource + return false; + } + } else { + if (!is_array($ts_path_filename_or_content) || empty($ts_path_filename_or_content)) { + // given translation not valid + return false; + } + $translation_in_file = false; } // iterate over lines - while(($line = fgets($file_handle, 65536)) !== false) { + while($parse_condition) { + // output and test line for translation in file + if ($translation_in_file) { + if (($line = fgets($file_handle, 65536)) === false) { + $parse_condition = false; + continue; + } + // output and test line for translation in array + } else { + $line = current($ts_path_filename_or_content); + $end_array_test = next($ts_path_filename_or_content); + if (false === $end_array_test) { + $parse_condition = false; + continue; + } + } + // store context name if (false !== strpos($line, '')) { preg_match_all("/()(.+)(<\/name>)/", $line, $context_name); @@ -273,7 +299,9 @@ function _parse_ts_file($ts_path_filename) $obsoleted_strings[] = $source_string[2][0] . ' context_name: '. $context_name[2][0]; } } - fclose($file_handle); + if ($translation_in_file) { + fclose($file_handle); + } $parsed_ts_file['num_of_original_str'] = $num_of_original_str; $parsed_ts_file['source_strings'] = $source_strings; -- cgit v1.2.1