Skip to content

Instantly share code, notes, and snippets.

@ebeauchamps
Forked from mwesten/date_localisation.patch
Last active August 29, 2015 14:14
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 ebeauchamps/c5c02ebd412cc609b92e to your computer and use it in GitHub Desktop.
Save ebeauchamps/c5c02ebd412cc609b92e to your computer and use it in GitHub Desktop.
Index: _app/config/default.settings.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- _app/config/default.settings.yaml (date 1419857417000)
+++ _app/config/default.settings.yaml (revision )
@@ -1,5 +1,6 @@
_license_key:
_language: en
+_iso_language: en_us
_site_name: Another Awesome Statamic Site
_content_root: _content
_site_url: http://statamic.com
\ No newline at end of file
Index: _config/settings.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- _config/settings.yaml (date 1419857417000)
+++ _config/settings.yaml (revision )
@@ -17,6 +17,10 @@
_language: en
+# Enter your country/language code from this list http://www.i18nguy.com/unicode/language-identifiers.html
+# and replace the `-` with an underscore `_` and put the codes in lower-case
+_iso_language: en_us
+
# Only set this if Statamic could not correctly guess your site's root
_site_root: /
@@ -122,7 +126,7 @@
- '*.com'
# Set to 'true' if you want to log system messages
-_log_enabled: false
+_log_enabled: true
# What's the least-serious level of message you wish to record to the log?
# Your available options:
Index: _app/core/api/date.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- _app/core/api/date.php (date 1419857417000)
+++ _app/core/api/date.php (revision )
@@ -24,6 +24,37 @@
/**
+ * Changes the dateformat from the 'date()' functioninto the format of the 'strftime()' function
+ * @param string $dateFormat date-format of the 'date()' function
+ * @return string date-format of the 'strftime()' function
+ */
+ public static function dateFormatToStrftime($dateFormat)
+ {
+ $caracs = array(
+ // Day - no strf eq : S
+ 'd' => '%d', 'D' => '%a', 'j' => '%e', 'l' => '%A', 'N' => '%u', 'w' => '%w', 'z' => '%j',
+ // Week - no date eq : %U, %W
+ 'W' => '%V',
+ // Month - no strf eq : n, t
+ 'F' => '%B', 'm' => '%m', 'M' => '%b',
+ // Year - no strf eq : L; no date eq : %C, %g
+ 'o' => '%G', 'Y' => '%Y', 'y' => '%y',
+ // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X
+ 'a' => '%P', 'A' => '%p', 'g' => '%l', 'h' => '%I', 'H' => '%H', 'i' => '%M', 's' => '%S',
+ // Timezone - no strf eq : e, I, P, Z
+ 'O' => '%z', 'T' => '%Z',
+ // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x
+ 'U' => '%s',
+ // Ordinal numbers
+ 'S' => '%O',
+ );
+
+ return strtr((string)$dateFormat, $caracs);
+ }
+
+
+
+ /**
* Formats a given $date (automatically resolving timestamps) to a given $format
*
* @param string $format Format to use (based on PHP's date formatting)
@@ -32,6 +63,75 @@
*/
public static function format($format, $date=NULL)
{
- return date($format, self::resolve(Helper::pick($date, time())));
+ $timestamp = self::resolve(Helper::pick($date, time()));
+ $newformat = self::dateFormatToStrftime($format);
+ setlocale(LC_TIME, (\Config::get('_iso_language','en_us')));
+ return self::getLocalisedOrdinals($newformat,$timestamp);
}
+
+ private static function getLocalisedOrdinals($dateformat, $timestamp)
+ {
+ // If we use Ordinals
+ if (strchr($dateformat,'%O')) {
+ // get currently set Locale
+ $locale = setlocale(LC_TIME, 0);
+ // return the language part for the locale, so we can construct the ordinals
+ $lang = substr($locale, 0, 2);
+ if (!empty($lang) && method_exists(get_called_class(),"_date_callback_" . $lang)) {
+ // Put in an unlikely placeholder.
+ $format = strtr($dateformat, array("%O" => "{}"));
+ $output = strftime($format, $timestamp);
+
+ $replace = array("{}" => call_user_func(array(get_called_class(),"_date_callback_" . $lang), strftime('%e', $timestamp)));
+ return strtr($output, $replace);
+ }
+ } else {
+ return strftime($dateformat, $timestamp);;
+ }
+ }
+
+ /**
+ * English. / English Ordinals
+ */
+ private static function _date_callback_en($number){
+ if ($number >= 10 && $number <= 19) {
+ return "th";
+ }
+ else {
+ switch ($number % 10) {
+ case 1:
+ return 'st';
+ break;
+ case 2:
+ return 'nd';
+ break;
+ case 3:
+ return 'rd';
+ break;
+ default:
+ return 'th';
+ break;
+ }
+ }
+ }
+
+ /**
+ * Nederlands. / Dutch Ordinals
+ */
+ private static function _date_callback_nl($number){
+ // see http://nl.wikipedia.org/wiki/Rangtelwoord
+ if ($number == 1) {
+ return 'ste';
+ }
+ else if ($number > 1 && $number <= 19) {
+ return "de";
+ }
+ else if ($number > 19 && $number < 100) {
+ return 'ste';
+ }
+ else {
+ return '';
+ }
+ }
+
}
\ No newline at end of file
Index: _config/settings.yaml.sample
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- _config/settings.yaml.sample (date 1419857417000)
+++ _config/settings.yaml.sample (revision )
@@ -17,6 +17,10 @@
_language: en
+# Enter your country/language code from this list http://www.i18nguy.com/unicode/language-identifiers.html
+# and replace the `-` with an underscore `_` and put the codes in lower-case
+_iso_language: en_us
+
# Only set this if Statamic could not correctly guess your site's root
#_site_root: /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment