WordPressを読む 16 /blog/wp-includes/pomo/translations.php
2014/12/09
目次
- 1 /blog/wp-includes/pomo/translations.php
- 2 読込元 : /blog/wp-includes/pomo/mo.php
- 3 /blog/wp-includes/pomo/entry.php
- 4 クラス Translations
- 5 Translations::クラス変数
- 6 Translations::add_entry()
- 7 Translations::add_entry_or_merge()
- 8 Translations::set_header()
- 9 Translations::set_headers()
- 10 Translations::get_header()
- 11 Translations::translate_entry()
- 12 Translations::translate()
- 13 Translations::select_plural_form()
- 14 Translations::get_plural_forms_count()
- 15 Translations::translate_plural()
- 16 Translations::merge_with()
- 17 Translations::merge_originals_with()
- 18 クラス Gettext_Translations
- 19 Gettext_Translations::gettext_select_plural_form()
- 20 Gettext_Translations::nplurals_and_expression_from_header()
- 21 Gettext_Translations::make_plural_form_function()
- 22 Gettext_Translations::parenthesize_plural_exression()
- 23 Gettext_Translations::make_headers()
- 24 Gettext_Translations::set_header()
- 25 クラス NOOP_Translations
- 26 NOOP_Translations::クラス変数
- 27 NOOP_Translations::add_entry()
- 28 NOOP_Translations::set_header()
- 29 NOOP_Translations::set_headers()
- 30 NOOP_Translations::get_header()
- 31 NOOP_Translations::translate_entry()
- 32 NOOP_Translations::translate()
- 33 NOOP_Translations::select_plural_form()
- 34 NOOP_Translations::get_plural_forms_count()
- 35 NOOP_Translations::translate_plural()
- 36 NOOP_Translations::function merge_with()
/blog/wp-includes/pomo/translations.php
読込元 : /blog/wp-includes/pomo/mo.php
1 2 3 4 5 6 7 8 9 10 | <?php /** * Class for a set of entries for translation and their associated headers * * @version $Id: translations.php 718 2012-10-31 00:32:02Z nbachiyski $ * @package pomo * @subpackage translations */ require_once dirname(__FILE__) . '/entry.php'; |
ファイル
/blog/wp-includes/pomo/entry.php
クラス Translations
PHP4のクラス書式
定義ファイル : /blog/wp-includes/pomo/translations.php
読込元 : /blog/wp-includes/pomo/mo.php
Translations::クラス変数
1 2 3 4 | if ( !class_exists( 'Translations' ) ): class Translations { var $entries = array(); var $headers = array(); |
メソッド
Translations::add_entry()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Add entry to the PO structure * * @param object &$entry * @return bool true on success, false if the entry doesn't have a key */ function add_entry($entry) { if (is_array($entry)) { $entry = new Translation_Entry($entry); } $key = $entry->key(); if (false === $key) return false; $this->entries[$key] = &$entry; return true; } |
メソッド
Translations::add_entry_or_merge()
1 2 3 4 5 6 7 8 9 10 11 12 | function add_entry_or_merge($entry) { if (is_array($entry)) { $entry = new Translation_Entry($entry); } $key = $entry->key(); if (false === $key) return false; if (isset($this->entries[$key])) $this->entries[$key]->merge_with($entry); else $this->entries[$key] = &$entry; return true; } |
メソッド
Translations::set_header()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Sets $header PO header to $value * * If the header already exists, it will be overwritten * * TODO: this should be out of this class, it is gettext specific * * @param string $header header name, without trailing : * @param string $value header value, without trailing \n */ function set_header($header, $value) { $this->headers[$header] = $value; } |
メソッド
Translations::set_headers()
1 2 3 4 5 | function set_headers($headers) { foreach($headers as $header => $value) { $this->set_header($header, $value); } } |
メソッド
Translations::get_header()
1 2 3 | function get_header($header) { return isset($this->headers[$header])? $this->headers[$header] : false; } |
メソッド
Translations::translate_entry()
1 2 3 4 | function translate_entry(&$entry) { $key = $entry->key(); return isset($this->entries[$key])? $this->entries[$key] : false; } |
メソッド
Translations::translate()
1 2 3 4 5 | function translate($singular, $context=null) { $entry = new Translation_Entry(array('singular' => $singular, 'context' => $context)); $translated = $this->translate_entry($entry); return ($translated && !empty($translated->translations))? $translated->translations[0] : $singular; } |
メソッド
Translations::select_plural_form()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Given the number of items, returns the 0-based index of the plural form to use * * Here, in the base Translations class, the common logic for English is implemented: * 0 if there is one element, 1 otherwise * * This function should be overrided by the sub-classes. For example MO/PO can derive the logic * from their headers. * * @param integer $count number of items */ function select_plural_form($count) { return 1 == $count? 0 : 1; } |
メソッド
Translations::get_plural_forms_count()
1 2 3 | function get_plural_forms_count() { return 2; } |
メソッド
Translations::translate_plural()
1 2 3 4 5 6 7 8 9 10 11 12 | function translate_plural($singular, $plural, $count, $context = null) { $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context)); $translated = $this->translate_entry($entry); $index = $this->select_plural_form($count); $total_plural_forms = $this->get_plural_forms_count(); if ($translated && 0 <= $index && $index < $total_plural_forms && is_array($translated->translations) && isset($translated->translations[$index])) return $translated->translations[$index]; else return 1 == $count? $singular : $plural; } |
メソッド
Translations::merge_with()
1 2 3 4 5 6 7 8 9 10 11 | /** * Merge $other in the current object. * * @param Object &$other Another Translation object, whose translations will be merged in this one * @return void **/ function merge_with(&$other) { foreach( $other->entries as $entry ) { $this->entries[$entry->key()] = $entry; } } |
メソッド
Translations::merge_originals_with()
1 2 3 4 5 6 7 8 | function merge_originals_with(&$other) { foreach( $other->entries as $entry ) { if ( !isset( $this->entries[$entry->key()] ) ) $this->entries[$entry->key()] = $entry; else $this->entries[$entry->key()]->merge_with($entry); } } |
1 | } |
クラス定義終了
クラス Gettext_Translations
PHP4のクラス書式
定義ファイル : /blog/wp-includes/pomo/translations.php
読込元 : /blog/wp-includes/pomo/mo.php
継承元 Translations
定義ファイル : /blog/wp-includes/pomo/translations.php
読込元 : /blog/wp-includes/pomo/mo.php
1 2 3 4 5 6 7 8 | class Gettext_Translations extends Translations { /** * The gettext implementation of select_plural_form. * * It lives in this class, because there are more than one descendand, which will use it and * they can't share it effectively. * */ |
メソッド
Gettext_Translations::gettext_select_plural_form()
1 2 3 4 5 6 7 8 | function gettext_select_plural_form($count) { if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) { list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); $this->_nplurals = $nplurals; $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); } return call_user_func($this->_gettext_select_plural_form, $count); } |
メソッド
Gettext_Translations::nplurals_and_expression_from_header()
1 2 3 4 5 6 7 8 9 | function nplurals_and_expression_from_header($header) { if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) { $nplurals = (int)$matches[1]; $expression = trim($this->parenthesize_plural_exression($matches[2])); return array($nplurals, $expression); } else { return array(2, 'n != 1'); } } |
メソッド
Gettext_Translations::make_plural_form_function()
1 2 3 4 5 6 7 8 9 10 11 | /** * Makes a function, which will return the right translation index, according to the * plural forms header */ function make_plural_form_function($nplurals, $expression) { $expression = str_replace('n', '$n', $expression); $func_body = " \$index = (int)($expression); return (\$index < $nplurals)? \$index : $nplurals - 1;"; return create_function('$n', $func_body); } |
メソッド
Gettext_Translations::parenthesize_plural_exression()
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 | /** * Adds parentheses to the inner parts of ternary operators in * plural expressions, because PHP evaluates ternary oerators from left to right * * @param string $expression the expression without parentheses * @return string the expression with parentheses added */ function parenthesize_plural_exression($expression) { $expression .= ';'; $res = ''; $depth = 0; for ($i = 0; $i < strlen($expression); ++$i) { $char = $expression[$i]; switch ($char) { case '?': $res .= ' ? ('; $depth++; break; case ':': $res .= ') : ('; break; case ';': $res .= str_repeat(')', $depth) . ';'; $depth= 0; break; default: $res .= $char; } } return rtrim($res, ';'); } |
メソッド
Gettext_Translations::make_headers()
1 2 3 4 5 6 7 8 9 10 11 12 | function make_headers($translation) { $headers = array(); // sometimes \ns are used instead of real new lines $translation = str_replace('\n', "\n", $translation); $lines = explode("\n", $translation); foreach($lines as $line) { $parts = explode(':', $line, 2); if (!isset($parts[1])) continue; $headers[trim($parts[0])] = trim($parts[1]); } return $headers; } |
メソッド
Gettext_Translations::set_header()
1 2 3 4 5 6 7 8 | function set_header($header, $value) { parent::set_header($header, $value); if ('Plural-Forms' == $header) { list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); $this->_nplurals = $nplurals; $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); } } |
1 2 | } endif; |
クラス定義終了
if文終了
クラス NOOP_Translations
PHP4のクラス書式
定義ファイル : /blog/wp-includes/pomo/translations.php
読込元 : /blog/wp-includes/pomo/mo.php
NOOP_Translations::クラス変数
1 2 3 4 5 6 7 | if ( !class_exists( 'NOOP_Translations' ) ): /** * Provides the same interface as Translations, but doesn't do anything */ class NOOP_Translations { var $entries = array(); var $headers = array(); |
メソッド
NOOP_Translations::add_entry()
1 2 3 | function add_entry($entry) { return true; } |
メソッド
NOOP_Translations::set_header()
1 2 | function set_header($header, $value) { } |
メソッド
NOOP_Translations::set_headers()
1 2 | function set_headers($headers) { } |
メソッド
NOOP_Translations::get_header()
1 2 3 | function get_header($header) { return false; } |
メソッド
NOOP_Translations::translate_entry()
1 2 3 | function translate_entry(&$entry) { return false; } |
メソッド
NOOP_Translations::translate()
1 2 3 | function translate($singular, $context=null) { return $singular; } |
メソッド
NOOP_Translations::select_plural_form()
1 2 3 | function select_plural_form($count) { return 1 == $count? 0 : 1; } |
メソッド
NOOP_Translations::get_plural_forms_count()
1 2 3 | function get_plural_forms_count() { return 2; } |
メソッド
NOOP_Translations::translate_plural()
1 2 3 | function translate_plural($singular, $plural, $count, $context = null) { return 1 == $count? $singular : $plural; } |
メソッド
NOOP_Translations::function merge_with()
1 2 | function merge_with(&$other) { } |
1 2 | } endif; |
クラス定義終了
if文終了