source: trunk/tools/translation_analysis.php @ 5312

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

improvement: ability to analyse a single language.

File size: 5.3 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
32if (!isset($_GET['lang']))
33{
34  echo '<a href="?lang=all">All languages</a><br><br>';
35  echo '<ul>';
36  foreach ($languages as $language)
37  {
38    if ($page['ref_compare'] == $language)
39    {
40      continue;
41    }
42    echo '<li><a href="?lang='.$language.'">'.$language.'</a></li>';
43  }
44  echo '</ul>';
45  exit();
46}
47else if (in_array($_GET['lang'], $languages))
48{
49  $languages = array($_GET['lang']);
50}
51
52$file_list = array('common', 'admin', 'install', 'upgrade');
53
54$metalang = array();
55
56// preload reference languages
57$metalang[ $page['ref_compare'] ] = load_metalang($page['ref_compare'], $file_list);
58$metalang[ $page['ref_default_values'] ] = load_metalang($page['ref_default_values'], $file_list);
59
60foreach ($languages as $language)
61{
62  if (in_array($language, array($page['ref_compare'], $page['ref_default_values'])))
63  {
64    continue;
65  }
66  echo '<h2>'.$language.'</h2>';
67  $metalang[$language] = load_metalang($language, $file_list);
68
69  foreach ($file_list as $file)
70  {
71    if (isset($metalang[ $language ][$file]))
72    {
73      $missing_keys = array_diff(
74        array_keys($metalang[ $page['ref_compare'] ][$file]),
75        array_keys($metalang[ $language ][$file])
76        );
77
78      $output_missing = '';
79      foreach ($missing_keys as $key)
80      {
81        $output_missing.= get_line_to_translate($file, $key);
82      }
83
84      // strings not "really" translated?
85      $output_duplicated = '';
86      $output_lost = '';
87      foreach (array_keys($metalang[$language][$file]) as $key)
88      {
89        $exceptions = array('Level 0');
90        if (in_array($key, $exceptions))
91        {
92          continue;
93        }
94
95        if (isset($validated_keys[$language]) and in_array($key, $validated_keys[$language]))
96        {
97          continue;
98        }
99       
100        $local_value = $metalang[$language][$file][$key];
101        if (!isset($metalang[ $page['ref_default_values'] ][$file][$key]))
102        {
103          $output_lost.= '#'.$key.'# does not exist in the reference language'."\n";
104        }
105        else
106        {
107          $ref_value = $metalang[ $page['ref_default_values'] ][$file][$key];
108          if ($local_value == $ref_value)
109          {
110            $output_duplicated.= get_line_to_translate($file, $key);
111          }
112        }
113      }
114
115      if ('' != $output_missing or '' != $output_duplicated)
116      {
117        $output = '';
118        if ('' != $output_missing)
119        {
120          $output.= "// missing translations\n".$output_missing;
121        }
122        if ('' != $output_duplicated)
123        {
124          $output.= "\n// untranslated yet\n".$output_duplicated;
125        }
126        echo '<h3>'.$file.'.lang.php</h3>';
127        echo '<textarea style="width:100%;height:150px;">'.$output.'</textarea>';
128      }
129
130      if ('' != $output_lost)
131      {
132        echo '<pre>'.$output_lost.'</pre>';
133      }
134    }
135    else
136    {
137      echo '<h3>'.$file.'.lang.php is missing</h3>';
138    }
139  }
140}
141
142function load_metalang($language, $file_list)
143{
144  global $lang, $user;
145 
146  $metalang = array();
147  foreach ($file_list as $file)
148  {
149    $lang = array();
150    $user['language'] = $language;
151    if (load_language($file.'.lang', '', array('language'=>$language, 'no_fallback'=>true)))
152    {
153      $metalang[$file] = $lang;
154    }
155  }
156  return $metalang;
157}
158
159function get_line_to_translate($file, $key)
160{
161  global $metalang, $page;
162 
163  $print_key = str_replace("'", '\\\'', $key);
164  $print_value = str_replace("'", '\\\'', $metalang[ $page['ref_default_values'] ][$file][$key]);
165  return '$'."lang['".$print_key."'] = '".$print_value."';\n";
166}
167?>
Note: See TracBrowser for help on using the repository browser.