Project

General

Profile

Feature #4467 » translate_array_to_po.php

Julio Montoya, 21/03/2012 12:45

 
1
<?php
2

    
3
exit;
4

    
5
//Source language
6
$dir = 'main/lang/english';
7

    
8
//Translate to
9
$to_dir = 'main/lang/spanish';
10

    
11
//The new po files will be saved in  $dir.'/LC_MESSAGES/';
12

    
13
///data/workspaces/tutorial/portal/lang/de_DE/LC_MESSAGES/portal.po
14
   
15
if (is_dir($dir)) {
16
    if ($dh = opendir($dir)) {
17
        while (($file = readdir($dh)) !== false) {
18
            $info = pathinfo($file);
19
            if ($info['extension'] != 'php') continue;
20
            
21
            echo "filename: $file : filetype: " . filetype($dir . $file) . "<br >";
22
            
23
            $translations = array();
24
            $filename = $dir.'/'.$file;
25
            var_dump($filename);
26
            
27
            $po = file($filename);  
28
            
29
            if (!file_exists($filename) || !file_exists($to_dir.'/'.$file)) {
30
                continue;
31
            }
32
            
33
            foreach ($po as $line) {
34
                $pos = strpos($line, '=');                
35
                if ($pos) {
36
                    $variable = (substr($line, 1, $pos-1));
37
                    $variable = trim($variable);
38
                    require $filename;
39
                    $my_variable_in_english = $$variable;
40
                    require $to_dir.'/'.$file;
41
                    $my_variable = $$variable;       
42
                    $translations[] = array('msgid' =>$my_variable_in_english, 'msgstr' =>$my_variable);
43
                }                   
44
            }
45
            //var_dump($translations);
46
            $info['filename'] = explode('.', $info['filename']);
47
            $info['filename'] = $info['filename'][0];
48
            $new_po_file = $to_dir.'/LC_MESSAGES/'.$info['filename'].'.po';
49
            
50
            var_dump($new_po_file);
51
            $fp = fopen($new_po_file, 'w');                            
52
            foreach($translations as $item) {   
53
                $line = 'msgid "'.addslashes($item['msgid']).'"'."\n";
54
                $line .= 'msgstr "'.addslashes($item['msgstr']).'"'."\n\n";                
55
                fwrite($fp, $line);
56
            }
57
            fclose($fp); 
58
        }
59
        closedir($dh);
60
    }
61
}
(1-1/2)