Beispiel #1
0
/**
 * Get languages names from Drupal's locale.inc.
 *
 * @param $file
 *   Full path name of file parsed
 * @param $save_callback
 *   Callback function used to save strings.
 * @param $api_version
 *   Drupal API version to work with.
 */
function _potx_find_language_names($file, $save_callback, $api_version = POTX_API_6)
{
    global $_potx_tokens, $_potx_lookup;
    foreach ($_potx_lookup[$api_version > POTX_API_5 ? '_locale_get_predefined_list' : '_locale_get_iso639_list'] as $ti) {
        // Search for the definition of _locale_get_predefined_list(), not where it is called.
        if ($_potx_tokens[$ti - 1][0] == T_FUNCTION) {
            break;
        }
    }
    $end = _potx_find_end_of_function($ti);
    $ti += 7;
    // function name, (, ), {, return, array, (
    while ($ti < $end) {
        while ($_potx_tokens[$ti][0] != T_ARRAY) {
            if (!is_array($_potx_tokens[$ti]) && $_potx_tokens[$ti] == ';') {
                // We passed the end of the list, break out to function level
                // to prevent an infinite loop.
                break 2;
            }
            $ti++;
        }
        $ti += 2;
        // array, (
        // Language names are context-less.
        $save_callback(_potx_format_quoted_string($_potx_tokens[$ti][1]), POTX_CONTEXT_NONE, $file, $_potx_tokens[$ti][2]);
    }
}
/**
 * Detect all occurances of t()-like calls from Drupal 7 (with context).
 *
 * These sequences are searched for:
 *   T_STRING("$function_name") + "(" + T_CONSTANT_ENCAPSED_STRING + ")"
 *   T_STRING("$function_name") + "(" + T_CONSTANT_ENCAPSED_STRING + ","
 *   and then an optional value for the replacements and an optional array
 *   for the options with an optional context key.
 *
 * @param $file
 *   Name of file parsed.
 * @param $save_callback
 *   Callback function used to save strings.
 * @param string $function_name
 * @param int $string_mode
 *   String mode to use: POTX_STRING_INSTALLER, POTX_STRING_RUNTIME or
 *   POTX_STRING_BOTH.
 *
 * @internal param $function_name The name of the function to look for (could be 't', '$t', 'st'*   The name of the function to look for (could be 't', '$t', 'st'
 *   or any other t-like function). Drupal 7 only supports context on t().
 */
function _potx_find_t_calls_with_context($file, $save_callback, $function_name = '_e', $string_mode = POTX_STRING_RUNTIME)
{
    global $_potx_tokens, $_potx_lookup;
    // Lookup tokens by function name.
    if (isset($_potx_lookup[$function_name])) {
        foreach ($_potx_lookup[$function_name] as $ti) {
            list($ctok, $par, $mid, $rig) = array($_potx_tokens[$ti], $_potx_tokens[$ti + 1], $_potx_tokens[$ti + 2], $_potx_tokens[$ti + 3]);
            list($type, $string, $line) = $ctok;
            if ($par == "(") {
                if (in_array($rig, array(")", ",")) && (is_array($mid) && $mid[0] == T_CONSTANT_ENCAPSED_STRING)) {
                    // By default, there is no context.
                    $domain = POTX_CONTEXT_NONE;
                    if ($rig == ',') {
                        if (in_array($function_name, array('_x', '_ex', 'esc_attr_x', 'esc_html_x'), true)) {
                            $domain_offset = 6;
                            $context_offset = 4;
                        } elseif ($function_name == '_n') {
                            $domain_offset = _potx_find_end_of_function($ti, '(', ')') - 1 - $ti;
                            $context_offset = false;
                            $text_plural = $_potx_tokens[$ti + 4][1];
                        } elseif ($function_name == '_nx') {
                            $domain_offset = _potx_find_end_of_function($ti, '(', ')') - 1 - $ti;
                            $context_offset = $domain_offset - 2;
                            $text_plural = $_potx_tokens[$ti + 4][1];
                        } else {
                            $domain_offset = 4;
                            $context_offset = false;
                        }
                        if (!isset($_potx_tokens[$ti + $domain_offset][1]) || !preg_match('#^(\'|")(.+)#', $_potx_tokens[$ti + $domain_offset][1])) {
                            continue;
                        } else {
                            $domain = trim($_potx_tokens[$ti + $domain_offset][1], "\"' ");
                        }
                        // exception for gettext calls with contexts
                        if (false !== $context_offset && isset($_potx_tokens[$ti + $context_offset])) {
                            if (!preg_match('#^(\'|")(.+)#', @$_potx_tokens[$ti + $context_offset][1])) {
                                $constant_val = @constant($_potx_tokens[$ti + $context_offset][1]);
                                if (!is_null($constant_val)) {
                                    $context = $constant_val;
                                } else {
                                    if (function_exists(@$_potx_tokens[$ti + $context_offset][1])) {
                                        $context = @$_potx_tokens[$ti + $context_offset][1]();
                                        if (empty($context)) {
                                            continue;
                                        }
                                    } else {
                                        continue;
                                    }
                                }
                            } else {
                                $context = trim($_potx_tokens[$ti + $context_offset][1], "\"' ");
                            }
                        } else {
                            $context = false;
                        }
                    }
                    if ($domain !== POTX_CONTEXT_ERROR && is_callable($save_callback, false, $callback_name)) {
                        // Only save if there was no error in context parsing.
                        call_user_func($save_callback, _potx_format_quoted_string($mid[1]), $domain, @strval($context), $file, $line, $string_mode);
                        if (isset($text_plural)) {
                            call_user_func($save_callback, _potx_format_quoted_string($text_plural), $domain, $context, $file, $line, $string_mode);
                        }
                    }
                } else {
                    // $function_name() found, but inside is something which is not a string literal.
                    _potx_marker_error($file, $line, $function_name, $ti, potx_t('The first parameter to @function() should be a literal string. There should be no variables, concatenation, constants or other non-literal strings there.', array('@function' => $function_name)), 'http://drupal.org/node/322732');
                }
            }
        }
    }
}