Ticket #3089: localized-keys-in-plugins.diff

File localized-keys-in-plugins.diff, 6.2 KB (added by nbachiyski, 6 years ago)
  • wp-includes/l10n.php

     
    9494        load_textdomain($domain, $mofile); 
    9595} 
    9696 
    97 ?> 
    98  No newline at end of file 
     97/** 
     98 *      Finds the best key match for a bunch of locales 
     99 * 
     100 *      Follows the freedesktop.org standart: 
     101 *      <a href="http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s04.html">Localized values for keys</a> 
     102 * 
     103 *      @param  mixed   $keys_array     each element must be an array, whose 
     104 *                                                              first (not zero) element is the locale and its second is the key value 
     105 *                                                              This format is suitable to come from for preg_match_all results 
     106 *      @param  string  $default_value  the value to be used if a match is not found 
     107 *      @return string  the most suitable localized value for the given locales 
     108 */ 
     109function l10n_match_key($keys_array, $default_value) {   
     110        $locale = l10n_parse_locale(get_locale(), true); 
     111        if (false === $locale) { 
     112                return $default_value; 
     113        } 
     114        $keys_dict = array(); 
     115        foreach ($keys_array as $item) { 
     116                $keys_dict[strtolower($item[1])] = $item[2]; 
     117        } 
     118                 
     119        $lcm = strtolower(l10n_locale_parts($locale, 'lcm')); 
     120        $lc = strtolower(l10n_locale_parts($locale, 'lc')); 
     121        $lm = strtolower(l10n_locale_parts($locale, 'lm')); 
     122        $l = strtolower(l10n_locale_parts($locale, 'l')); 
     123 
     124        if (array_key_exists($lcm, $keys_dict)) { 
     125                return $keys_dict[$lcm]; 
     126        } elseif (array_key_exists($lc, $keys_dict)) { 
     127                return $keys_dict[$lc]; 
     128        } elseif (array_key_exists($lm, $keys_dict)) { 
     129                return $keys_dict[$lm]; 
     130        } elseif (array_key_exists($l, $keys_dict)) { 
     131                return $keys_dict[$l]; 
     132        } 
     133        return $default_value; 
     134} 
     135 
     136/** 
     137 *      Parses a ln_CN.Encoding@Modifier string and returns hash with its parts 
     138 *       
     139 *      @param  string  $locale the locale string 
     140 *      @param  bool    $strip_encoding whether to return empty encoding part 
     141 *      @return mixed   hash with following keys: 
     142 *                                              lang, country, encoding, modifier  
     143 */ 
     144function l10n_parse_locale($locale, $strip_encoding = false) { 
     145        $locale_re = "^([a-z]{2})(?:_([a-z]{2}))?(?:\.([a-z0-9-]+))?(?:\@([a-z0-9-]+))?$"; 
     146        if (preg_match("|$locale_re|i", $locale, $matches)) { 
     147                if ($strip_encoding) { 
     148                        $matches[3] = ''; 
     149                } 
     150                return array('locale' => $matches[0], 'lang' => $matches[1], 'country' => $matches[2], 
     151                                'encoding' => $matches[3], 'modifier' => $matches[4]); 
     152        } else { 
     153                return false; 
     154        } 
     155} 
     156 
     157/** 
     158 *      Extracts parts of an already parsed locale 
     159 * 
     160 *      @param  mixed   $parsed_locale locale string parsed with {@link ln10_parse_locale} 
     161 *      @param  string  $format some characters are replaced with their corresponding locale parts: 
     162 *                                              'l'     -> language 
     163 *                                              'c'     -> _country 
     164 *                                              'e'     -> .encoding 
     165 *                                              'm'     -> @modofier 
     166 *      @return string  the parts defined by $format, joined together in s string 
     167 * 
     168 *      @example <code>l10n_locale_parts(l10n_parse_locale('bg_BG@baba'), 'lem')</code> returns <code>bg@baba</code> 
     169 */ 
     170function l10n_locale_parts($parsed_locale, $format) { 
     171        $result = ''; 
     172        $format = preg_split('//', $format, -1, PREG_SPLIT_NO_EMPTY); 
     173        foreach ($format as $c) { 
     174                switch ($c) { 
     175                        case 'l': $result .= !empty($parsed_locale['lang'])? $parsed_locale['lang'] : ''; break; 
     176                        case 'c': $result .= !empty($parsed_locale['country'])? '_'.$parsed_locale['country'] : ''; break; 
     177                        case 'e': $result .= !empty($parsed_locale['encoding'])? '.'.$parsed_locale['encoding'] : ''; break; 
     178                        case 'm': $result .= !empty($parsed_locale['modifier'])? '@'.$parsed_locale['modifier'] : ''; break; 
     179                } 
     180        } 
     181        return $result; 
     182} 
     183 
     184?> 
  • wp-admin/admin-functions.php

     
    16821682 
    16831683function get_plugin_data($plugin_file) { 
    16841684        $plugin_data = implode('', file($plugin_file)); 
     1685 
    16851686        preg_match("|Plugin Name:(.*)|i", $plugin_data, $plugin_name); 
     1687        preg_match_all("|Plugin Name\[(.+?)\]:(.*)|i", $plugin_data, $locale_names, PREG_SET_ORDER); 
     1688         
    16861689        preg_match("|Plugin URI:(.*)|i", $plugin_data, $plugin_uri); 
     1690        preg_match_all("|Plugin URI\[(.+?)\]:(.*)|i", $plugin_data, $locale_uris, PREG_SET_ORDER); 
     1691         
    16871692        preg_match("|Description:(.*)|i", $plugin_data, $description); 
     1693        preg_match_all("|Description\[(.+?)\]:(.*)|i", $plugin_data, $locale_descs, PREG_SET_ORDER); 
     1694         
    16881695        preg_match("|Author:(.*)|i", $plugin_data, $author_name); 
     1696        preg_match_all("|Author\[(.+?)\]:(.*)|i", $plugin_data, $locale_authors, PREG_SET_ORDER); 
     1697         
    16891698        preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri); 
     1699        preg_match_all("|Author URI\[(.+?)\]:(.*)|i", $plugin_data, $locale_author_uris, PREG_SET_ORDER); 
     1700 
    16901701        if (preg_match("|Version:(.*)|i", $plugin_data, $version)) 
    16911702                $version = trim($version[1]); 
    16921703        else 
    16931704                $version = ''; 
    16941705 
    1695         $description = wptexturize(trim($description[1])); 
    16961706 
    1697         $name = $plugin_name[1]; 
    1698         $name = trim($name); 
     1707        $name = trim(l10n_match_key($locale_names, $plugin_name[1])); 
     1708        $plugin_uri = trim(l10n_match_key($locale_uris, $plugin_uri[1])); 
     1709        $description = trim(l10n_match_key($locale_descs, $description[1])); 
     1710        $author = trim(l10n_match_key($locale_authors, $author_name[1])); 
     1711        $author_uri = trim(l10n_match_key($locale_author_uris, $author_uri[1])); 
     1712 
     1713         
     1714        $description = wptexturize($description); 
     1715 
    16991716        $plugin = $name; 
    1700         if ('' != $plugin_uri[1] && '' != $name) { 
    1701                 $plugin = '<a href="' . trim($plugin_uri[1]) . '" title="'.__('Visit plugin homepage').'">'.$plugin.'</a>'; 
     1717        if ('' != $plugin_uri && '' != $name) { 
     1718                $plugin = '<a href="' . $plugin_uri . '" title="'.__('Visit plugin homepage').'">'.$plugin.'</a>'; 
    17021719        } 
    17031720 
    1704         if ('' == $author_uri[1]) { 
    1705                 $author = trim($author_name[1]); 
    1706         } else { 
    1707                 $author = '<a href="' . trim($author_uri[1]) . '" title="'.__('Visit author homepage').'">' . trim($author_name[1]) . '</a>'; 
     1721        if ('' != $author_uri) { 
     1722                $author = '<a href="' . $author_uri . '" title="'.__('Visit author homepage').'">' . $author . '</a>'; 
    17081723        } 
    17091724 
    1710         return array ('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1]); 
     1725        return array ('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version); 
    17111726} 
    17121727 
    17131728function get_plugins() {