Browse Source

Improved show times.

Martin Edenhofer 10 years ago
parent
commit
53c2089b62

+ 16 - 1
app/assets/javascripts/app/lib/app_post/i18n.js.coffee

@@ -24,6 +24,11 @@ class App.i18n
       _instance ?= new _i18nSingleton
     _instance.timestamp( args, offset )
 
+  @translateDate: ( args, offset = 0 ) ->
+    if _instance == undefined
+      _instance ?= new _i18nSingleton
+    _instance.date( args, offset )
+
   @get: ->
     if _instance == undefined
       _instance ?= new _i18nSingleton
@@ -44,6 +49,7 @@ class _i18nSingleton extends Spine.Module
 
   constructor: ( locale ) ->
     @map = {}
+    @dateFormat      = 'yyyy-mm-dd'
     @timestampFormat = 'yyyy-mm-dd HH:MM'
 
     # observe if text has been translated
@@ -114,6 +120,10 @@ class _i18nSingleton extends Spine.Module
         if data.timestampFormat
           @timestampFormat = data.timestampFormat
 
+        # set date format
+        if data.dateFormat
+          @dateFormat = data.dateFormat
+
         # load translation collection
         for object in data.list
 
@@ -176,7 +186,13 @@ class _i18nSingleton extends Spine.Module
       .replace(/>/g, '>')
       .replace(/\x22/g, '"')
 
+  date: ( time, offset ) =>
+    @convert(time, offset, @dateFormat)
+
   timestamp: ( time, offset ) =>
+    @convert(time, offset, @timestampFormat)
+
+  convert: ( time, offset, format ) =>
     s = ( num, digits ) ->
       while num.toString().length < digits
         num = "0" + num
@@ -194,7 +210,6 @@ class _i18nSingleton extends Spine.Module
     S = timeObject.getSeconds()
     M = timeObject.getMinutes()
     H = timeObject.getHours()
-    format = @timestampFormat
     format = format.replace /dd/, s( d, 2 )
     format = format.replace /d/, d
     format = format.replace /mm/, s( m, 2 )

+ 3 - 0
app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee

@@ -28,6 +28,9 @@ class App.PrettyDate
       diff = diff.toString().replace('-', '')
       diff = parseFloat(diff)
 
+    if direction is 'past' && !escalation && diff > ( 60 * 60 * 24 * 14 )
+      return App.i18n.translateDate(time)
+
     # days
     string = ''
     count = 0

+ 8 - 0
app/models/translation.rb

@@ -31,9 +31,17 @@ class Translation < ApplicationModel
       :de => 'dd.mm.yyyy HH:MM',
     }
     timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default
+
+    date_map_default = 'yyyy-mm-dd'
+    date_map = {
+      :de => 'dd.mm.yyyy',
+    }
+    date = date_map[ locale.to_sym ] || date_map_default
+
     return {
       :list            => list,
       :timestampFormat => timestamp,
+      :dateFormat      => date,
     }
   end
 

+ 18 - 0
public/assets/tests/ui.js

@@ -43,6 +43,19 @@ test( "check pretty date", function() {
   result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 10.5 ) );
   equal( result, '10 days ago', '10.5 days')
 
+  result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 30 ) );
+  var pastDate = new Date(current - ( 60000 * 60 * 24 * 30 ))
+  var dd = pastDate.getDate();
+  if( dd<10 ) {
+      dd = '0' + dd
+  }
+  var mm = pastDate.getMonth() + 1;
+  if( mm<10 ) {
+      mm = '0' + mm
+  }
+  var yyyy = pastDate.getFullYear();
+  equal( result, yyyy+'-'+mm+'-'+dd, '30 days')
+
   // future
   current = new Date()
   result = App.PrettyDate.humanTime( current );
@@ -72,5 +85,10 @@ test( "check pretty date", function() {
   result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 2.5 ) );
   equal( result, 'in 2 days 12 hours', 'in 2.5 days')
 
+  result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 5.5 ) );
+  equal( result, 'in 5 days 12 hours', 'in 30.5 days')
+
+  result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 30.5 ) );
+  equal( result, 'in 30 days', 'in 30.5 days')
 
 });