Skip to content

Commit

Permalink
1st attempt to support i18n in dates and times
Browse files Browse the repository at this point in the history
  • Loading branch information
kubamracek committed Mar 7, 2013
1 parent 052887c commit 2e76e33
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 158 deletions.
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
//= require ./discourse/helpers/i18n_helpers
//= require ./discourse

//= require ./locales/date_locales.js

// Stuff we need to load first
//= require_tree ./discourse/mixins
//= require ./discourse/views/view
Expand Down
25 changes: 13 additions & 12 deletions app/assets/javascripts/discourse/helpers/application_helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/*global humaneDate:true */

/**
Breaks up a long string
Expand Down Expand Up @@ -162,7 +160,7 @@ Handlebars.registerHelper('avatar', function(user, options) {
Handlebars.registerHelper('unboundDate', function(property, options) {
var dt;
dt = new Date(Ember.Handlebars.get(this, property, options));
return dt.format("{d} {Mon}, {yyyy} {hh}:{mm}");
return dt.format("long");
});

/**
Expand All @@ -176,9 +174,9 @@ Handlebars.registerHelper('editDate', function(property, options) {
dt = Date.create(Ember.Handlebars.get(this, property, options));
yesterday = new Date() - (60 * 60 * 24 * 1000);
if (yesterday > dt.getTime()) {
return dt.format("{d} {Mon}, {yyyy} {hh}:{mm}");
return dt.format("long");
} else {
return humaneDate(dt);
return dt.relative();
}
});

Expand Down Expand Up @@ -215,7 +213,7 @@ Handlebars.registerHelper('number', function(property, options) {
@for Handlebars
**/
Handlebars.registerHelper('date', function(property, options) {
var displayDate, dt, fiveDaysAgo, fullReadable, humanized, leaveAgo, val;
var displayDate, dt, fiveDaysAgo, oneMinuteAgo, fullReadable, humanized, leaveAgo, val;
if (property.hash) {
if (property.hash.leaveAgo) {
leaveAgo = property.hash.leaveAgo === "true";
Expand All @@ -229,23 +227,26 @@ Handlebars.registerHelper('date', function(property, options) {
return new Handlebars.SafeString("—");
}
dt = new Date(val);
fullReadable = dt.format("{d} {Mon}, {yyyy} {hh}:{mm}");
fullReadable = dt.format("long");
displayDate = "";
fiveDaysAgo = (new Date()) - 432000000;
if (fiveDaysAgo > (dt.getTime())) {
oneMinuteAgo = (new Date()) - 60000;
if (oneMinuteAgo <= dt.getTime() && dt.getTime() <= (new Date())) {
displayDate = Em.String.i18n("now");
} else if (fiveDaysAgo > (dt.getTime())) {
if ((new Date()).getFullYear() !== dt.getFullYear()) {
displayDate = dt.format("{d} {Mon} '{yy}");
displayDate = dt.format("short");
} else {
displayDate = dt.format("{d} {Mon}");
displayDate = dt.format("short_no_year");
}
} else {
humanized = humaneDate(dt);
humanized = dt.relative();
if (!humanized) {
return "";
}
displayDate = humanized;
if (!leaveAgo) {
displayDate = displayDate.replace(' ago', '');
displayDate = (dt.millisecondsAgo()).duration();
}
}
return new Handlebars.SafeString("<span class='date' title='" + fullReadable + "'>" + displayDate + "</span>");
Expand Down
134 changes: 0 additions & 134 deletions app/assets/javascripts/external/humane.js

This file was deleted.

36 changes: 36 additions & 0 deletions app/assets/javascripts/locales/date_locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// fix EN locale
Date.getLocale('en').short_no_year = '{d} {Mon}';

// create CS locale
Date.addLocale('cs', {
'plural': true,
'capitalizeUnit': false,
'months': 'ledna,února,března,dubna,května,června,července,srpna,září,října,listopadu,prosince',
'weekdays': 'neděle,pondělí,úterý,středa,čtvrtek,pátek,sobota',
'units': 'milisekund:a|y||ou|ami,sekund:a|y||ou|ami,minut:a|y||ou|ami,hodin:a|y||ou|ami,den|dny|dnů|dnem|dny,týden|týdny|týdnů|týdnem|týdny,měsíc:|e|ů|em|emi,rok|roky|let|rokem|lety',
'short': '{d}. {month} {yyyy}',
'short_no_year': '{d}. {month}',
'long': '{d}. {month} {yyyy} {H}:{mm}',
'full': '{weekday} {d}. {month} {yyyy} {H}:{mm}:{ss}',
'relative': function(num, unit, ms, format) {
var numberWithUnit, last = num.toString().slice(-1);
var mult;
if (format === 'past' || format === 'future') {
if (num === 1) mult = 3;
else mult = 4;
} else {
if (num === 1) mult = 0;
else if (num >= 2 && num <= 4) mult = 1;
else mult = 2;
}
numberWithUnit = num + ' ' + this.units[(mult * 8) + unit];
switch(format) {
case 'duration': return numberWithUnit;
case 'past': return 'před ' + numberWithUnit;
case 'future': return 'za ' + numberWithUnit;
}
}
});

// set the current date locale
Date.setLocale(I18n.locale);
1 change: 1 addition & 0 deletions config/locales/client.cs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cs:
you: "Vy"
ok: "ok"
or: "nebo"
now: "právě teď"

suggested_topics:
title: "Doporučená témata"
Expand Down
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ en:
you: "You"
ok: "ok"
or: "or"
now: "just now"

suggested_topics:
title: "Suggested Topics"
Expand Down
13 changes: 1 addition & 12 deletions lib/age_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ module AgeWords
def self.age_words(secs)
return "&mdash;" if secs.blank?

mins = (secs / 60.0)
hours = (mins / 60.0)
days = (hours / 24.0)
months = (days / 30.0)
years = (months / 12.0)

return "#{years.floor}y" if years > 1
return "#{months.floor}mo" if months > 1
return "#{days.floor}d" if days > 1
return "#{hours.floor}h" if hours > 1
return "&lt; 1m" if mins < 1
return "#{mins.floor}m"
return FreedomPatches::Rails4.distance_of_time_in_words(Time.now, Time.now + secs)
end

end

0 comments on commit 2e76e33

Please sign in to comment.