PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-locale.php

https://gitlab.com/WPonEB/WPonEB
PHP | 394 lines | 133 code | 36 blank | 225 comment | 7 complexity | 5a68f1d5624265055c796ad6bb3feabf MD5 | raw file
  1. <?php
  2. /**
  3. * Locale API: WP_Locale class
  4. *
  5. * @package WordPress
  6. * @subpackage i18n
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Core class used to store translated data for a locale.
  11. *
  12. * @since 2.1.0
  13. * @since 4.6.0 Moved to its own file from wp-includes/locale.php.
  14. */
  15. class WP_Locale {
  16. /**
  17. * Stores the translated strings for the full weekday names.
  18. *
  19. * @since 2.1.0
  20. * @var array
  21. */
  22. public $weekday;
  23. /**
  24. * Stores the translated strings for the one character weekday names.
  25. *
  26. * There is a hack to make sure that Tuesday and Thursday, as well
  27. * as Sunday and Saturday, don't conflict. See init() method for more.
  28. *
  29. * @see WP_Locale::init() for how to handle the hack.
  30. *
  31. * @since 2.1.0
  32. * @var array
  33. */
  34. public $weekday_initial;
  35. /**
  36. * Stores the translated strings for the abbreviated weekday names.
  37. *
  38. * @since 2.1.0
  39. * @var array
  40. */
  41. public $weekday_abbrev;
  42. /**
  43. * Stores the default start of the week.
  44. *
  45. * @since 4.4.0
  46. * @var string
  47. */
  48. public $start_of_week;
  49. /**
  50. * Stores the translated strings for the full month names.
  51. *
  52. * @since 2.1.0
  53. * @var array
  54. */
  55. public $month;
  56. /**
  57. * Stores the translated strings for the month names in genitive case, if the locale specifies.
  58. *
  59. * @since 4.4.0
  60. * @var array
  61. */
  62. public $month_genitive;
  63. /**
  64. * Stores the translated strings for the abbreviated month names.
  65. *
  66. * @since 2.1.0
  67. * @var array
  68. */
  69. public $month_abbrev;
  70. /**
  71. * Stores the translated strings for 'am' and 'pm'.
  72. *
  73. * Also the capitalized versions.
  74. *
  75. * @since 2.1.0
  76. * @var array
  77. */
  78. public $meridiem;
  79. /**
  80. * The text direction of the locale language.
  81. *
  82. * Default is left to right 'ltr'.
  83. *
  84. * @since 2.1.0
  85. * @var string
  86. */
  87. public $text_direction = 'ltr';
  88. /**
  89. * The thousands separator and decimal point values used for localizing numbers.
  90. *
  91. * @since 2.3.0
  92. * @var array
  93. */
  94. public $number_format;
  95. /**
  96. * Constructor which calls helper methods to set up object variables.
  97. *
  98. * @since 2.1.0
  99. */
  100. public function __construct() {
  101. $this->init();
  102. $this->register_globals();
  103. }
  104. /**
  105. * Sets up the translated strings and object properties.
  106. *
  107. * The method creates the translatable strings for various
  108. * calendar elements. Which allows for specifying locale
  109. * specific calendar names and text direction.
  110. *
  111. * @since 2.1.0
  112. *
  113. * @global string $text_direction
  114. */
  115. public function init() {
  116. // The Weekdays
  117. $this->weekday[0] = /* translators: weekday */ __('Sunday');
  118. $this->weekday[1] = /* translators: weekday */ __('Monday');
  119. $this->weekday[2] = /* translators: weekday */ __('Tuesday');
  120. $this->weekday[3] = /* translators: weekday */ __('Wednesday');
  121. $this->weekday[4] = /* translators: weekday */ __('Thursday');
  122. $this->weekday[5] = /* translators: weekday */ __('Friday');
  123. $this->weekday[6] = /* translators: weekday */ __('Saturday');
  124. // The first letter of each day.
  125. $this->weekday_initial[ __( 'Sunday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Sunday initial' );
  126. $this->weekday_initial[ __( 'Monday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'M', 'Monday initial' );
  127. $this->weekday_initial[ __( 'Tuesday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Tuesday initial' );
  128. $this->weekday_initial[ __( 'Wednesday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'W', 'Wednesday initial' );
  129. $this->weekday_initial[ __( 'Thursday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'T', 'Thursday initial' );
  130. $this->weekday_initial[ __( 'Friday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'F', 'Friday initial' );
  131. $this->weekday_initial[ __( 'Saturday' ) ] = /* translators: one-letter abbreviation of the weekday */ _x( 'S', 'Saturday initial' );
  132. // Abbreviations for each day.
  133. $this->weekday_abbrev[__('Sunday')] = /* translators: three-letter abbreviation of the weekday */ __('Sun');
  134. $this->weekday_abbrev[__('Monday')] = /* translators: three-letter abbreviation of the weekday */ __('Mon');
  135. $this->weekday_abbrev[__('Tuesday')] = /* translators: three-letter abbreviation of the weekday */ __('Tue');
  136. $this->weekday_abbrev[__('Wednesday')] = /* translators: three-letter abbreviation of the weekday */ __('Wed');
  137. $this->weekday_abbrev[__('Thursday')] = /* translators: three-letter abbreviation of the weekday */ __('Thu');
  138. $this->weekday_abbrev[__('Friday')] = /* translators: three-letter abbreviation of the weekday */ __('Fri');
  139. $this->weekday_abbrev[__('Saturday')] = /* translators: three-letter abbreviation of the weekday */ __('Sat');
  140. // The Months
  141. $this->month['01'] = /* translators: month name */ __( 'January' );
  142. $this->month['02'] = /* translators: month name */ __( 'February' );
  143. $this->month['03'] = /* translators: month name */ __( 'March' );
  144. $this->month['04'] = /* translators: month name */ __( 'April' );
  145. $this->month['05'] = /* translators: month name */ __( 'May' );
  146. $this->month['06'] = /* translators: month name */ __( 'June' );
  147. $this->month['07'] = /* translators: month name */ __( 'July' );
  148. $this->month['08'] = /* translators: month name */ __( 'August' );
  149. $this->month['09'] = /* translators: month name */ __( 'September' );
  150. $this->month['10'] = /* translators: month name */ __( 'October' );
  151. $this->month['11'] = /* translators: month name */ __( 'November' );
  152. $this->month['12'] = /* translators: month name */ __( 'December' );
  153. // The Months, genitive
  154. $this->month_genitive['01'] = /* translators: month name, genitive */ _x( 'January', 'genitive' );
  155. $this->month_genitive['02'] = /* translators: month name, genitive */ _x( 'February', 'genitive' );
  156. $this->month_genitive['03'] = /* translators: month name, genitive */ _x( 'March', 'genitive' );
  157. $this->month_genitive['04'] = /* translators: month name, genitive */ _x( 'April', 'genitive' );
  158. $this->month_genitive['05'] = /* translators: month name, genitive */ _x( 'May', 'genitive' );
  159. $this->month_genitive['06'] = /* translators: month name, genitive */ _x( 'June', 'genitive' );
  160. $this->month_genitive['07'] = /* translators: month name, genitive */ _x( 'July', 'genitive' );
  161. $this->month_genitive['08'] = /* translators: month name, genitive */ _x( 'August', 'genitive' );
  162. $this->month_genitive['09'] = /* translators: month name, genitive */ _x( 'September', 'genitive' );
  163. $this->month_genitive['10'] = /* translators: month name, genitive */ _x( 'October', 'genitive' );
  164. $this->month_genitive['11'] = /* translators: month name, genitive */ _x( 'November', 'genitive' );
  165. $this->month_genitive['12'] = /* translators: month name, genitive */ _x( 'December', 'genitive' );
  166. // Abbreviations for each month.
  167. $this->month_abbrev[ __( 'January' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Jan', 'January abbreviation' );
  168. $this->month_abbrev[ __( 'February' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Feb', 'February abbreviation' );
  169. $this->month_abbrev[ __( 'March' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Mar', 'March abbreviation' );
  170. $this->month_abbrev[ __( 'April' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Apr', 'April abbreviation' );
  171. $this->month_abbrev[ __( 'May' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'May', 'May abbreviation' );
  172. $this->month_abbrev[ __( 'June' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Jun', 'June abbreviation' );
  173. $this->month_abbrev[ __( 'July' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Jul', 'July abbreviation' );
  174. $this->month_abbrev[ __( 'August' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Aug', 'August abbreviation' );
  175. $this->month_abbrev[ __( 'September' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Sep', 'September abbreviation' );
  176. $this->month_abbrev[ __( 'October' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Oct', 'October abbreviation' );
  177. $this->month_abbrev[ __( 'November' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Nov', 'November abbreviation' );
  178. $this->month_abbrev[ __( 'December' ) ] = /* translators: three-letter abbreviation of the month */ _x( 'Dec', 'December abbreviation' );
  179. // The Meridiems
  180. $this->meridiem['am'] = __('am');
  181. $this->meridiem['pm'] = __('pm');
  182. $this->meridiem['AM'] = __('AM');
  183. $this->meridiem['PM'] = __('PM');
  184. // Numbers formatting
  185. // See https://secure.php.net/number_format
  186. /* translators: $thousands_sep argument for https://secure.php.net/number_format, default is , */
  187. $thousands_sep = __( 'number_format_thousands_sep' );
  188. if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
  189. // Replace space with a non-breaking space to avoid wrapping.
  190. $thousands_sep = str_replace( ' ', '&nbsp;', $thousands_sep );
  191. } else {
  192. // PHP < 5.4.0 does not support multiple bytes in thousands separator.
  193. $thousands_sep = str_replace( array( '&nbsp;', '&#160;' ), ' ', $thousands_sep );
  194. }
  195. $this->number_format['thousands_sep'] = ( 'number_format_thousands_sep' === $thousands_sep ) ? ',' : $thousands_sep;
  196. /* translators: $dec_point argument for https://secure.php.net/number_format, default is . */
  197. $decimal_point = __( 'number_format_decimal_point' );
  198. $this->number_format['decimal_point'] = ( 'number_format_decimal_point' === $decimal_point ) ? '.' : $decimal_point;
  199. // Set text direction.
  200. if ( isset( $GLOBALS['text_direction'] ) )
  201. $this->text_direction = $GLOBALS['text_direction'];
  202. /* translators: 'rtl' or 'ltr'. This sets the text direction for WordPress. */
  203. elseif ( 'rtl' == _x( 'ltr', 'text direction' ) )
  204. $this->text_direction = 'rtl';
  205. if ( 'rtl' === $this->text_direction && strpos( get_bloginfo( 'version' ), '-src' ) ) {
  206. $this->text_direction = 'ltr';
  207. add_action( 'all_admin_notices', array( $this, 'rtl_src_admin_notice' ) );
  208. }
  209. }
  210. /**
  211. * Outputs an admin notice if the /build directory must be used for RTL.
  212. *
  213. * @since 3.8.0
  214. */
  215. public function rtl_src_admin_notice() {
  216. /* translators: %s: Name of the directory (build) */
  217. echo '<div class="error"><p>' . sprintf( __( 'The %s directory of the develop repository must be used for RTL.' ), '<code>build</code>' ) . '</p></div>';
  218. }
  219. /**
  220. * Retrieve the full translated weekday word.
  221. *
  222. * Week starts on translated Sunday and can be fetched
  223. * by using 0 (zero). So the week starts with 0 (zero)
  224. * and ends on Saturday with is fetched by using 6 (six).
  225. *
  226. * @since 2.1.0
  227. *
  228. * @param int $weekday_number 0 for Sunday through 6 Saturday
  229. * @return string Full translated weekday
  230. */
  231. public function get_weekday($weekday_number) {
  232. return $this->weekday[$weekday_number];
  233. }
  234. /**
  235. * Retrieve the translated weekday initial.
  236. *
  237. * The weekday initial is retrieved by the translated
  238. * full weekday word. When translating the weekday initial
  239. * pay attention to make sure that the starting letter does
  240. * not conflict.
  241. *
  242. * @since 2.1.0
  243. *
  244. * @param string $weekday_name
  245. * @return string
  246. */
  247. public function get_weekday_initial($weekday_name) {
  248. return $this->weekday_initial[$weekday_name];
  249. }
  250. /**
  251. * Retrieve the translated weekday abbreviation.
  252. *
  253. * The weekday abbreviation is retrieved by the translated
  254. * full weekday word.
  255. *
  256. * @since 2.1.0
  257. *
  258. * @param string $weekday_name Full translated weekday word
  259. * @return string Translated weekday abbreviation
  260. */
  261. public function get_weekday_abbrev($weekday_name) {
  262. return $this->weekday_abbrev[$weekday_name];
  263. }
  264. /**
  265. * Retrieve the full translated month by month number.
  266. *
  267. * The $month_number parameter has to be a string
  268. * because it must have the '0' in front of any number
  269. * that is less than 10. Starts from '01' and ends at
  270. * '12'.
  271. *
  272. * You can use an integer instead and it will add the
  273. * '0' before the numbers less than 10 for you.
  274. *
  275. * @since 2.1.0
  276. *
  277. * @param string|int $month_number '01' through '12'
  278. * @return string Translated full month name
  279. */
  280. public function get_month($month_number) {
  281. return $this->month[zeroise($month_number, 2)];
  282. }
  283. /**
  284. * Retrieve translated version of month abbreviation string.
  285. *
  286. * The $month_name parameter is expected to be the translated or
  287. * translatable version of the month.
  288. *
  289. * @since 2.1.0
  290. *
  291. * @param string $month_name Translated month to get abbreviated version
  292. * @return string Translated abbreviated month
  293. */
  294. public function get_month_abbrev($month_name) {
  295. return $this->month_abbrev[$month_name];
  296. }
  297. /**
  298. * Retrieve translated version of meridiem string.
  299. *
  300. * The $meridiem parameter is expected to not be translated.
  301. *
  302. * @since 2.1.0
  303. *
  304. * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
  305. * @return string Translated version
  306. */
  307. public function get_meridiem($meridiem) {
  308. return $this->meridiem[$meridiem];
  309. }
  310. /**
  311. * Global variables are deprecated.
  312. *
  313. * For backward compatibility only.
  314. *
  315. * @deprecated For backward compatibility only.
  316. *
  317. * @global array $weekday
  318. * @global array $weekday_initial
  319. * @global array $weekday_abbrev
  320. * @global array $month
  321. * @global array $month_abbrev
  322. *
  323. * @since 2.1.0
  324. */
  325. public function register_globals() {
  326. $GLOBALS['weekday'] = $this->weekday;
  327. $GLOBALS['weekday_initial'] = $this->weekday_initial;
  328. $GLOBALS['weekday_abbrev'] = $this->weekday_abbrev;
  329. $GLOBALS['month'] = $this->month;
  330. $GLOBALS['month_abbrev'] = $this->month_abbrev;
  331. }
  332. /**
  333. * Checks if current locale is RTL.
  334. *
  335. * @since 3.0.0
  336. * @return bool Whether locale is RTL.
  337. */
  338. public function is_rtl() {
  339. return 'rtl' == $this->text_direction;
  340. }
  341. /**
  342. * Register date/time format strings for general POT.
  343. *
  344. * Private, unused method to add some date/time formats translated
  345. * on wp-admin/options-general.php to the general POT that would
  346. * otherwise be added to the admin POT.
  347. *
  348. * @since 3.6.0
  349. */
  350. public function _strings_for_pot() {
  351. /* translators: localized date format, see https://secure.php.net/date */
  352. __( 'F j, Y' );
  353. /* translators: localized time format, see https://secure.php.net/date */
  354. __( 'g:i a' );
  355. /* translators: localized date and time format, see https://secure.php.net/date */
  356. __( 'F j, Y g:i a' );
  357. }
  358. }