[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/ -> test_locales.php (source)

   1  <?php
   2  
   3  /**
   4   * @group locales
   5   */
   6  class GP_Test_Locales extends GP_UnitTestCase {
   7  
   8      const SKIP_LOCALES = [
   9          // Actual: n != 1
  10          // Expected: n > 1
  11          'ak', 'am', 'as', 'bho', 'bn', 'fuc', 'ful', 'gu', 'hi', 'hy', 'kn', 'mg',
  12          'nso', 'pcm', 'pt-ao', 'si', 'wa', 'zul',
  13  
  14          // Actual: n > 1
  15          // Expected: n != 1
  16          'orm', 'tr', 'tuk',
  17  
  18          // Actual: n != 1
  19          // Expected: 0
  20          'bm', 'jv', 'ka', 'mya', 'nqo', 'sah', 'yor',
  21  
  22          // Actual: n > 1
  23          // Expected: (n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91) ? 0 : ((n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92) ? 1 : ((((n % 10 == 3 || n % 10 == 4) || n % 10 == 9) && (n % 100 < 10 || n % 100 > 19) && (n % 100 < 70 || n % 100 > 79) && (n % 100 < 90 || n % 100 > 99)) ? 2 : ((n != 0 && n % 1000000 == 0) ? 3 : 4)))
  24          'br',
  25  
  26          // Actual: n != 1
  27          // Expected: n != 1 && n != 2 && n != 3 && (n % 10 == 4 || n % 10 == 6 || n % 10 == 9)
  28          'ceb', 'tl',
  29  
  30          // Actual: (n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3
  31          // Expected: (n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((n == 3) ? 3 : ((n == 6) ? 4 : 5))))
  32          'cy',
  33  
  34          // Actual: n != 1
  35          // Expected: (n == 1) ? 0 : ((n == 2) ? 1 : ((n > 10 && n % 10 == 0) ? 2 : 3))
  36          'he',
  37  
  38          // Actual: n > 1
  39          // Expected: 0
  40          'id',
  41  
  42          // Actual: 0
  43          // Expected: n != 1
  44          'kir', 'uz',
  45  
  46          // Actual: n != 1
  47          // Expected: 0
  48          'tir',
  49  
  50          // Actual: n > 1
  51          // Expected: n >= 2 && (n < 11 || n > 99)
  52          'tzm',
  53  
  54          // Actual: n > 1
  55          // Expected: (n == 0 || n == 1) ? 0 : ((n != 0 && n % 1000000 == 0) ? 1 : 2)
  56          'fr', 'fr-be', 'fr-ca', 'fr-ch',
  57      ];
  58  
  59  	public function test_class_exists() {
  60          $this->assertTrue( class_exists( 'GP_Locales' ) );
  61      }
  62  
  63      /**
  64       * Tests syntax of ISO codes for language and country for a locale.
  65       *
  66       * @dataProvider data_provider_locales
  67       *
  68       * @param GP_locale $locale The locale to test.
  69       */
  70  	public function test_locale_iso_codes( $locale ) {
  71          if ( isset( $locale->lang_code_iso_639_1 ) ) {
  72              $this->assertSame( 2, strlen( $locale->lang_code_iso_639_1 ), "ISO 639-1 length check for $locale" );
  73              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->lang_code_iso_639_1 ), "ISO 639-1 format check for $locale" );
  74          }
  75  
  76          if ( isset( $locale->lang_code_iso_639_2 ) ) {
  77              $this->assertSame( 3, strlen( $locale->lang_code_iso_639_2 ), "ISO 639-2 length check for $locale" );
  78              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->lang_code_iso_639_2 ), "ISO 639-2 format check for $locale" );
  79          }
  80  
  81          if ( isset( $locale->lang_code_iso_639_3 ) ) {
  82              $this->assertSame( 3, strlen( $locale->lang_code_iso_639_3 ), "ISO 639-3 length check for $locale" );
  83              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->lang_code_iso_639_3 ), "ISO 639-3 format check for $locale" );
  84          }
  85  
  86          if ( isset( $locale->country_code ) ) {
  87              $this->assertSame( 2, strlen( $locale->country_code ), "ISO 3166 length check for $locale" );
  88              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->country_code ), "ISO 3166 format check for $locale" );
  89          }
  90      }
  91  
  92      /**
  93       * Tests plural form of a locale.
  94       *
  95       * @dataProvider data_provider_locales
  96       *
  97       * @param GP_locale $locale The locale to test.
  98       */
  99  	public function test_locale_plural( $locale ) {
 100          if ( in_array( $locale->slug, self::SKIP_LOCALES, true ) ) {
 101              $this->markTestSkipped( "{$locale->slug} skipped, requires research" );
 102          }
 103  
 104          $id = '';
 105          if ( isset( $locale->lang_code_iso_639_1, $locale->country_code ) ) {
 106              $id = $locale->lang_code_iso_639_1 . '_' . strtoupper( $locale->country_code );
 107          }
 108  
 109          if ( ! $id && isset( $locale->lang_code_iso_639_1 ) ) {
 110              $id = $locale->lang_code_iso_639_1;
 111          }
 112  
 113          if ( ! $id && isset( $locale->lang_code_iso_639_2 ) ) {
 114              $id = $locale->lang_code_iso_639_2;
 115          }
 116  
 117          if ( ! $id && isset( $locale->lang_code_iso_639_3 ) ) {
 118              $id = $locale->lang_code_iso_639_3;
 119          }
 120  
 121          $language = Gettext\Languages\Language::getById( $id );
 122          if ( $language ) {
 123              $this->assertSame( $language->formula, $locale->plural_expression, $locale->slug );
 124              $this->assertSame( count( $language->categories ), $locale->nplurals, $locale->slug );
 125              return;
 126          }
 127  
 128          // Try again with different ID.
 129          $id = '';
 130          if ( isset( $locale->lang_code_iso_639_1 ) ) {
 131              $id = $locale->lang_code_iso_639_1;
 132          }
 133  
 134          if ( ! $id && isset( $locale->lang_code_iso_639_2 ) ) {
 135              $id = $locale->lang_code_iso_639_2;
 136          }
 137  
 138          if ( ! $id && isset( $locale->lang_code_iso_639_3 ) ) {
 139              $id = $locale->lang_code_iso_639_3;
 140          }
 141  
 142          $language = Gettext\Languages\Language::getById( $id );
 143          if ( $language ) {
 144              $this->assertSame( $language->formula, $locale->plural_expression, $locale->slug );
 145              $this->assertSame( count( $language->categories ), $locale->nplurals, $locale->slug );
 146              return;
 147          }
 148  
 149          // Try again with different ID.
 150          $id = '';
 151          if ( isset( $locale->lang_code_iso_639_2 ) ) {
 152              $id = $locale->lang_code_iso_639_2;
 153          }
 154  
 155          if ( ! $id && isset( $locale->lang_code_iso_639_3 ) ) {
 156              $id = $locale->lang_code_iso_639_3;
 157          }
 158  
 159          $language = Gettext\Languages\Language::getById( $id );
 160          if ( ! $language ) {
 161              $this->markTestSkipped( "{$locale->slug} not found in CLDR" );
 162          }
 163  
 164          $this->assertSame( $language->formula, $locale->plural_expression, $locale->slug );
 165          $this->assertSame( count( $language->categories ), $locale->nplurals, $locale->slug );
 166      }
 167  
 168  	public function data_provider_locales() {
 169          unset( $GLOBALS['gp_locales'] );
 170          $locales = GP_Locales::locales();
 171  
 172          $data = [];
 173  
 174          foreach ( $locales as $locale ) {
 175              $data[] = [ $locale ];
 176          }
 177  
 178          return $data;
 179      }
 180  }


Generated: Thu Nov 21 01:01:07 2024 Cross-referenced by PHPXref 0.7.1