source: trunk/tools/translation_analysis.php @ 5309

Last change on this file since 5309 was 5309, checked in by plg, 14 years ago

improvement: clean output for lost language keys

File size: 4.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24define('PHPWG_ROOT_PATH', '../');
25include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
26include_once( PHPWG_ROOT_PATH.'tools/translation_validated.inc.php' );
27$languages = array_keys(get_languages());
28
29$page['ref_compare'] = 'en_UK';
30$page['ref_default_values'] = 'en_UK';
31
32$file_list = array('common', 'admin', 'install', 'upgrade');
33
34$metalang = array();
35
36// preload reference languages
37$metalang[ $page['ref_compare'] ] = load_metalang($page['ref_compare'], $file_list);
38$metalang[ $page['ref_default_values'] ] = load_metalang($page['ref_default_values'], $file_list);
39
40foreach ($languages as $language)
41{
42  if (in_array($language, array($page['ref_compare'], $page['ref_default_values'])))
43  {
44    continue;
45  }
46  echo '<h2>'.$language.'</h2>';
47  $metalang[$language] = load_metalang($language, $file_list);
48
49  foreach ($file_list as $file)
50  {
51    if (isset($metalang[ $language ][$file]))
52    {
53      $missing_keys = array_diff(
54        array_keys($metalang[ $page['ref_compare'] ][$file]),
55        array_keys($metalang[ $language ][$file])
56        );
57
58      $output_missing = '';
59      foreach ($missing_keys as $key)
60      {
61        $output_missing.= get_line_to_translate($file, $key);
62      }
63
64      // strings not "really" translated?
65      $output_duplicated = '';
66      $output_lost = '';
67      foreach (array_keys($metalang[$language][$file]) as $key)
68      {
69        $exceptions = array('Level 0');
70        if (in_array($key, $exceptions))
71        {
72          continue;
73        }
74
75        if (isset($validated_keys[$language]) and in_array($key, $validated_keys[$language]))
76        {
77          continue;
78        }
79       
80        $local_value = $metalang[$language][$file][$key];
81        if (!isset($metalang[ $page['ref_default_values'] ][$file][$key]))
82        {
83          $output_lost.= '#'.$key.'# does not exist in the reference language'."\n";
84        }
85        else
86        {
87          $ref_value = $metalang[ $page['ref_default_values'] ][$file][$key];
88          if ($local_value == $ref_value)
89          {
90            $output_duplicated.= get_line_to_translate($file, $key);
91          }
92        }
93      }
94
95      if ('' != $output_missing or '' != $output_duplicated)
96      {
97        $output = '';
98        if ('' != $output_missing)
99        {
100          $output.= "// missing translations\n".$output_missing;
101        }
102        if ('' != $output_duplicated)
103        {
104          $output.= "\n// untranslated yet\n".$output_duplicated;
105        }
106        echo '<h3>'.$file.'.lang.php</h3>';
107        echo '<textarea style="width:100%;height:150px;">'.$output.'</textarea>';
108      }
109
110      if ('' != $output_lost)
111      {
112        echo '<pre>'.$output_lost.'</pre>';
113      }
114    }
115    else
116    {
117      echo '<h3>'.$file.'.lang.php is missing</h3>';
118    }
119  }
120}
121
122function load_metalang($language, $file_list)
123{
124  global $lang, $user;
125 
126  $metalang = array();
127  foreach ($file_list as $file)
128  {
129    $lang = array();
130    $user['language'] = $language;
131    if (load_language($file.'.lang', '', array('language'=>$language, 'no_fallback'=>true)))
132    {
133      $metalang[$file] = $lang;
134    }
135  }
136  return $metalang;
137}
138
139function get_line_to_translate($file, $key)
140{
141  global $metalang, $page;
142 
143  $print_key = str_replace("'", '\\\'', $key);
144  $print_value = str_replace("'", '\\\'', $metalang[ $page['ref_default_values'] ][$file][$key]);
145  return '$'."lang['".$print_key."'] = '".$print_value."';\n";
146}
147?>
Note: See TracBrowser for help on using the repository browser.