[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> l10n.php (source)

   1  <?php
   2  /**
   3   * Localization. Loading and using translations.
   4   */
   5  
   6  
   7  function get_locale() {
   8      global $locale;
   9  
  10      if (isset($locale))
  11          return apply_filters( 'locale', $locale );
  12  
  13      // GP_LANG is defined in gp-config.
  14      if ( defined('GP_LANG') )
  15          $locale = GP_LANG;
  16          
  17      // TODO: get locale from DB
  18  
  19      if (empty($locale))
  20          $locale = 'en_US';
  21  
  22      $locale = apply_filters('locale', $locale);
  23  
  24      return $locale;
  25  }
  26  
  27  function translate( $text, $domain = 'default' ) {
  28      $translations = &get_translations_for_domain( $domain );
  29      return apply_filters('gettext', $translations->translate($text), $text, $domain);
  30  }
  31  
  32  function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
  33      $translations = &get_translations_for_domain( $domain );
  34      return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain);
  35  }
  36  
  37  function __( $text, $domain = 'default' ) {
  38      return translate( $text, $domain );
  39  }
  40  
  41  function esc_attr__( $text, $domain = 'default' ) {
  42      return esc_attr( translate( $text, $domain ) );
  43  }
  44  
  45  function esc_html__( $text, $domain = 'default' ) {
  46      return esc_html( translate( $text, $domain ) );
  47  }
  48  
  49  function _e( $text, $domain = 'default' ) {
  50      echo translate( $text, $domain );
  51  }
  52  
  53  function esc_attr_e( $text, $domain = 'default' ) {
  54      echo esc_attr( translate( $text, $domain ) );
  55  }
  56  
  57  function esc_html_e( $text, $domain = 'default' ) {
  58      echo esc_html( translate( $text, $domain ) );
  59  }
  60  
  61  function _x( $single, $context, $domain = 'default' ) {
  62      return translate_with_gettext_context( $single, $context, $domain );
  63  }
  64  
  65  function esc_attr_x( $single, $context, $domain = 'default' ) {
  66      return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
  67  }
  68  
  69  function esc_html_x( $single, $context, $domain = 'default' ) {
  70      return esc_html( translate_with_gettext_context( $single, $context, $domain ) );
  71  }
  72  
  73  function _n($single, $plural, $number, $domain = 'default') {
  74      $translations = &get_translations_for_domain( $domain );
  75      $translation = $translations->translate_plural( $single, $plural, $number );
  76      return apply_filters( 'ngettext', $translation, $single, $plural, $number );
  77  }
  78  
  79  function _nx($single, $plural, $number, $context, $domain = 'default') {
  80      $translations = &get_translations_for_domain( $domain );
  81      $translation = $translations->translate_plural( $single, $plural, $number, $context );
  82      return apply_filters( 'ngettext_with_context ', $translation, $single, $plural, $number, $context );
  83  }
  84  
  85  function _n_noop( $single, $plural, $number = 1, $domain = 'default' ) {
  86      return array( $single, $plural );
  87  }
  88  
  89  function load_textdomain( $domain, $mofile ) {
  90      global $l10n;
  91  
  92      if ( !is_readable( $mofile ) ) return;
  93      
  94      $mo = new MO();
  95      $mo->import_from_file( $mofile );
  96  
  97      if ( isset( $l10n [$domain] ) )
  98          $mo->merge_with( $l10n[$domain] );
  99          
 100      $l10n[$domain] = &$mo;
 101  }
 102  
 103  function load_default_textdomain() {
 104      $locale = get_locale();
 105  
 106      $mofile = GP_LANG_PATH . "/$locale.mo";
 107  
 108      load_textdomain('default', $mofile);
 109  }
 110  
 111  /**
 112   * Returns the Translations instance for a domain. If there isn't one,
 113   * returns empty Translations instance.
 114   *
 115   * @since 1.0.0
 116   *
 117   * @param string $domain
 118   * @return object A Translation instance
 119   */
 120  function get_translations_for_domain( $domain ) {
 121      global $l10n;
 122      $empty = new Translations;
 123      return isset($l10n[$domain])? $l10n[$domain] : $empty;
 124  }


Generated: Thu May 24 03:59:35 2012 Hosted by follow the white rabbit.