Skip to content

Instantly share code, notes, and snippets.

@vijaycs85
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijaycs85/89cc769e0171ba9f0364 to your computer and use it in GitHub Desktop.
Save vijaycs85/89cc769e0171ba9f0364 to your computer and use it in GitHub Desktop.
diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
index 16f68b0..bfe0211 100644
--- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
+++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
@@ -90,7 +90,7 @@ protected function prepareTimezone($timezone) {
* The formatted value of the date.
*/
public function format($format, $settings = array()) {
- $settings['langcode'] = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode;
+ $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode;
// Format the date and catch errors.
try {
// Encode markers that should be translated. 'A' becomes
@@ -104,8 +104,24 @@ public function format($format, $settings = array()) {
// Call date_format().
$format = parent::format($format);
- $callback = function($matches) use ($settings) {
- return self::getFormatTranslation($settings['langcode'], $matches[1], $matches[2]);
+ $callback = function($matches) use ($langcode) {
+ $code = $matches[1];
+ $string = $matches[2];
+ if (!isset($this->formatTranslationCache[$langcode][$code][$string])) {
+ $options = array('langcode' => $langcode);
+
+ if ($code == 'F') {
+ $options['context'] = 'Long month name';
+ }
+
+ if ($code == '') {
+ $this->formatTranslationCache[$langcode][$code][$string] = $string;
+ }
+ else {
+ $this->formatTranslationCache[$langcode][$code][$string] = t($string, array(), $options);
+ }
+ }
+ return $this->formatTranslationCache[$langcode][$code][$string];
};
// Translate the marked sequences.
$value = preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', $callback, $format);
@@ -116,31 +132,4 @@ public function format($format, $settings = array()) {
return $value;
}
- /**
- * Translates a formatted date string.
- *
- * @param $langcode
- * @param $code
- * @param $string
- *
- * @return mixed
- */
- public function getFormatTranslation($langcode, $code, $string) {
- if (!isset($this->formatTranslationCache[$langcode][$code][$string])) {
- $options = array('langcode' => $langcode);
-
- if ($code == 'F') {
- $options['context'] = 'Long month name';
- }
-
- if ($code == '') {
- $this->formatTranslationCache[$langcode][$code][$string] = $string;
- }
- else {
- $this->formatTranslationCache[$langcode][$code][$string] = t($string, array(), $options);
- }
- }
- return $this->formatTranslationCache[$langcode][$code][$string];
- }
-
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment