[Jifty-commit] r4284 - in jifty/trunk: lib/Jifty lib/Jifty/Plugin/SkeletonApp share/web/static/js

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Oct 21 12:52:49 EDT 2007


Author: clkao
Date: Sun Oct 21 12:52:49 2007
New Revision: 4284

Added:
   jifty/trunk/share/web/static/js/loc.js
Modified:
   jifty/trunk/lib/Jifty/Plugin/SkeletonApp/View.pm
   jifty/trunk/lib/Jifty/View/Static/Handler.pm
   jifty/trunk/lib/Jifty/Web.pm

Log:
Support serving json version of po and localization in javascript
space.


Modified: jifty/trunk/lib/Jifty/Plugin/SkeletonApp/View.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/SkeletonApp/View.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/SkeletonApp/View.pm	Sun Oct 21 12:52:49 2007
@@ -60,8 +60,13 @@
         title { _($title) };
         Jifty->web->include_css;
         Jifty->web->include_javascript;
-      };
 
+        if (Jifty->config->framework('L10N')->{js}) {
+            # js l10n
+            my $current_lang = Jifty::I18N->get_current_language || 'en';
+            outs_raw(qq{<script type="text/javascript">Localization.init({dict_path: '/static/js/dict', lang: '$current_lang'});</script>});
+        }
+    };
 };
 
 private template 'heading_in_wrapper' => sub {

Modified: jifty/trunk/lib/Jifty/View/Static/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/View/Static/Handler.pm	(original)
+++ jifty/trunk/lib/Jifty/View/Static/Handler.pm	Sun Oct 21 12:52:49 2007
@@ -192,8 +192,9 @@
         # MIME::Types returns application/javascript for .js, but Opera
         # chokes on ajax-fetched JS that has a type other than the one below
         # JSAN.js fetches JS via Ajax when it loads JSAN modules
-        'js' => 'application/x-javascript',
-        'htc' => 'text/x-component',
+        'js'   => 'application/x-javascript',
+        'json' => 'application/json; charset=UTF-8',
+        'htc'  => 'text/x-component',
     );
 
     return ($type_override{$1})

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Sun Oct 21 12:52:49 2007
@@ -49,6 +49,7 @@
     scriptaculous/controls.js
     formatDate.js
     template_declare.js
+    loc.js
     jifty.js
     jifty_utils.js
     jifty_subs.js

Added: jifty/trunk/share/web/static/js/loc.js
==============================================================================
--- (empty file)
+++ jifty/trunk/share/web/static/js/loc.js	Sun Oct 21 12:52:49 2007
@@ -0,0 +1,77 @@
+Localization = Object.extend(new Object(), {
+    init: function(params) {
+        this.lang = params.lang || 'en'
+        if (params["dict_path"]) {
+            this.dict_path = params["dict_path"]
+            this.dict = this.load_dict(this.lang)
+        }
+        Ext.onReady(function() {
+            Localization.show_dates_as_local_time();
+        })
+    },
+    load_dict: function(lang) {
+        var d;
+        new Ajax.Request(
+            this.dict_path + "/" + this.lang + ".json",
+            {
+                method: 'get',
+                asynchronous: false,
+                onComplete: function(t, obj) {
+                    eval("d = " + t.responseText);
+                }
+            }
+        );
+        return d;
+    },
+    loc: function(str) {
+        var dict = this.dict
+        if (dict[str]) {
+            return dict[str]
+        }
+        return str
+    },
+
+    show_dates_as_local_time: function() {
+        var spans = document.getElementsByTagName('span');
+        for (var i=0; i<spans.length; i++) {
+            if (spans[i].className.match(/\bdatetime\b/i)) {
+                var d = this.get_local_time_for_date(spans[i].title);
+                if (d != 'NaN') {
+                    spans[i].innerHTML = d
+                }
+            }
+        }
+    },
+
+    get_local_time_for_date: function(time) {
+        system_date = new Date(time);
+        user_date = new Date();
+        delta_minutes = Math.floor((user_date - system_date) / (60 * 1000));
+        if (Math.abs(delta_minutes) <= (7*24*60)) {
+            distance = this.distance_of_time_in_words(delta_minutes);
+            if (delta_minutes < 0) {
+                return distance + _(' from now');
+            } else {
+                return distance + _(' ago');
+            }
+        } else {
+            return system_date.toLocaleDateString();
+        }
+    },
+
+    distance_of_time_in_words: function(minutes) {
+        if (minutes.isNaN) return "";
+        minutes = Math.abs(minutes);
+        if (minutes < 1) return _('less than a minute');
+        if (minutes < 50) return _(minutes + ' minute' + (minutes == 1 ? '' : 's'));
+        if (minutes < 90) return _('about one hour');
+        if (minutes < 1080) return (Math.round(minutes / 60) + ' hours');
+        if (minutes < 1440) return _('one day');
+        if (minutes < 2880) return _('about one day');
+        else return (Math.round(minutes / 1440) + _(' days'))
+    }
+
+})
+
+_ = Localization.loc.bind(Localization)
+


More information about the Jifty-commit mailing list