1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
<?php
/**
* This file a contains a message group implementation.
*
* @file
* @author Niklas Laxström
* @copyright Copyright © 2010-2013, Niklas Laxström
* @license GPL-2.0+
*/
/**
* This class implements default behavior for file based message groups.
*
* File based message groups are primary type of groups at translatewiki.net,
* while other projects may use mainly page translation message groups, or
* custom type of message groups.
* @ingroup MessageGroup
*/
class FileBasedMessageGroup extends MessageGroupBase {
protected $reverseCodeMap;
/**
* Constructs a FileBasedMessageGroup from any normal message group.
* Useful for doing special Gettext exports from any group.
* @param $group MessageGroup
* @return FileBasedMessageGroup
*/
public static function newFromMessageGroup( $group ) {
$conf = array(
'BASIC' => array(
'class' => 'FileBasedMessageGroup',
'id' => $group->getId(),
'label' => $group->getLabel(),
'namespace' => $group->getNamespace(),
),
'FILES' => array(
'sourcePattern' => '',
'targetPattern' => '',
),
);
return MessageGroupBase::factory( $conf );
}
public function exists() {
return $this->getFFS()->exists();
}
public function load( $code ) {
/** @var $ffs FFS */
$ffs = $this->getFFS();
$data = $ffs->read( $code );
return $data ? $data['MESSAGES'] : array();
}
/**
* @param string $code Language code.
* @return string
* @throws MWException
*/
public function getSourceFilePath( $code ) {
if ( $this->isSourceLanguage( $code ) ) {
$pattern = $this->getFromConf( 'FILES', 'definitionFile' );
if ( $pattern !== null ) {
return $this->replaceVariables( $pattern, $code );
}
}
$pattern = $this->getFromConf( 'FILES', 'sourcePattern' );
if ( $pattern === null ) {
throw new MWException( 'No source file pattern defined.' );
}
return $this->replaceVariables( $pattern, $code );
}
public function getTargetFilename( $code ) {
// Check if targetPattern explicitly defined
$pattern = $this->getFromConf( 'FILES', 'targetPattern' );
if ( $pattern !== null ) {
return $this->replaceVariables( $pattern, $code );
}
// Check if definitionFile is explicitly defined
if ( $this->isSourceLanguage( $code ) ) {
$pattern = $this->getFromConf( 'FILES', 'definitionFile' );
}
// Fallback to sourcePattern which must be defined
if ( $pattern === null ) {
$pattern = $this->getFromConf( 'FILES', 'sourcePattern' );
}
if ( $pattern === null ) {
throw new MWException( 'No source file pattern defined.' );
}
// For exports, the scripts take output directory. We want to
// return a path where the prefix is current directory instead
// of full path of the source location.
$pattern = str_replace( '%GROUPROOT%', '.', $pattern );
return $this->replaceVariables( $pattern, $code );
}
/**
* @param string $pattern
* @param string $code Language code.
* @return string
* @since 2014.02 Made public
*/
public function replaceVariables( $pattern, $code ) {
global $IP, $wgTranslateGroupRoot;
$variables = array(
'%CODE%' => $this->mapCode( $code ),
'%MWROOT%' => $IP,
'%GROUPROOT%' => $wgTranslateGroupRoot,
);
wfRunHooks( 'TranslateMessageGroupPathVariables', array( $this, &$variables ) );
return str_replace( array_keys( $variables ), array_values( $variables ), $pattern );
}
/**
* @param string $code Language code.
* @return string
*/
public function mapCode( $code ) {
if ( !isset( $this->conf['FILES']['codeMap'] ) ) {
return $code;
}
if ( isset( $this->conf['FILES']['codeMap'][$code] ) ) {
return $this->conf['FILES']['codeMap'][$code];
} else {
if ( !isset( $this->reverseCodeMap ) ) {
$this->reverseCodeMap = array_flip( $this->conf['FILES']['codeMap'] );
}
if ( isset( $this->reverseCodeMap[$code] ) ) {
return 'x-invalidLanguageCode';
}
return $code;
}
}
}
|