RubyYamlFFS.php
Go to the documentation of this file.00001 <?php
00002
00009 class RubyYamlFFS extends YamlFFS {
00010 protected static $pluralWords = array(
00011 'zero' => 1,
00012 'one' => 1,
00013 'many' => 1,
00014 'few' => 1,
00015 'other' => 1,
00016 'two' => 1
00017 );
00018
00019 public function getFileExtensions() {
00020 return array( '.yml', '.yaml' );
00021 }
00022
00031 public function flattenPlural( $messages ) {
00032
00033 $pluralKeys = false;
00034 $nonPluralKeys = false;
00035 foreach ( $messages as $key => $value ) {
00036 if ( is_array( $value ) ) {
00037 # Plurals can only happen in the lowest level of the structure
00038 return false;
00039 }
00040
00041 # Check if we find any reserved plural keyword
00042 if ( isset( self::$pluralWords[$key] ) ) {
00043 $pluralKeys = true;
00044 } else {
00045 $nonPluralKeys = true;
00046 }
00047 }
00048
00049 # No plural keys at all, we can skip
00050 if ( !$pluralKeys ) {
00051 return false;
00052 }
00053
00054 # Mixed plural keys with other keys, should not happen
00055 if ( $nonPluralKeys ) {
00056 $keys = implode( ', ', array_keys( $messages ) );
00057 throw new MWException( "Reserved plural keywords mixed with other keys: $keys." );
00058 }
00059
00060 $pls = '{{PLURAL';
00061 foreach ( $messages as $key => $value ) {
00062 if ( $key === 'other' ) {
00063 continue;
00064 }
00065
00066 $pls .= "|$key=$value";
00067 }
00068
00069
00070 $other = isset( $messages['other'] ) ? '|' . $messages['other'] : '';
00071 $pls .= "$other}}";
00072
00073 return $pls;
00074 }
00075
00084 public function unflattenPlural( $key, $message ) {
00085
00086 if ( strpos( $message, '{{PLURAL' ) === false ) {
00087 return array( $key => $message );
00088 }
00089
00090
00091
00092
00093
00094 $regex = '~\{[a-zA-Z_-]+}~';
00095 $placeholders = array();
00096 $match = array();
00097
00098 while ( preg_match( $regex, $message, $match ) ) {
00099 $uniqkey = TranslateUtils::getPlaceholder();
00100 $placeholders[$uniqkey] = $match[0];
00101 $search = preg_quote( $match[0], '~' );
00102 $message = preg_replace( "~$search~", $uniqkey, $message );
00103 }
00104
00105
00106 $regex = '~\{\{PLURAL\|(.*?)}}~s';
00107 $matches = array();
00108 $match = array();
00109
00110 while ( preg_match( $regex, $message, $match ) ) {
00111 $uniqkey = TranslateUtils::getPlaceholder();
00112 $matches[$uniqkey] = $match;
00113 $message = preg_replace( $regex, $uniqkey, $message );
00114 }
00115
00116
00117 if ( !count( $matches ) ) {
00118 return false;
00119 }
00120
00121
00122 $alts = array();
00123
00124
00125
00126
00127
00128
00129 $pluralChoice = implode( '|', array_keys( self::$pluralWords ) );
00130 $regex = "~($pluralChoice)\s*=\s*(.+)~s";
00131 foreach ( $matches as $ph => $plu ) {
00132 $forms = explode( '|', $plu[1] );
00133
00134 foreach ( $forms as $form ) {
00135 if ( $form === '' ) {
00136 continue;
00137 }
00138
00139 $match = array();
00140 if ( preg_match( $regex, $form, $match ) ) {
00141 $formWord = "$key.{$match[1]}";
00142 $value = $match[2];
00143 } else {
00144 $formWord = "$key.other";
00145 $value = $form;
00146 }
00147
00148 if ( !isset( $alts[$formWord] ) ) {
00149 $alts[$formWord] = $message;
00150 }
00151
00152 $string = $alts[$formWord];
00153 $alts[$formWord] = str_replace( $ph, $value, $string );
00154 }
00155 }
00156
00157
00158 foreach ( $alts as &$value ) {
00159 $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value );
00160 }
00161
00162 if ( !isset( $alts["$key.other"] ) ) {
00163 wfWarn( "Other not set for key $key" );
00164
00165 return false;
00166 }
00167
00168 return $alts;
00169 }
00170 }