Skip to content

Commit

Permalink
Initial moment.js locale auto detection
Browse files Browse the repository at this point in the history
Beginning of #56
  • Loading branch information
poblabs committed Feb 4, 2019
1 parent 626d6c1 commit 145d7c1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions bin/user/belchertown.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import syslog
import sys
import locale

import weewx
import weecfg
Expand Down Expand Up @@ -78,6 +79,9 @@ def get_extension_list(self, timespan, db_lookup):
# Multiplying by -1 will reverse the number sign and keep 0 (not -0). https://stackoverflow.com/a/14053631/1177153
highcharts_timezoneoffset = moment_js_utc_offset * -1

# Get the system locale for use with moment.js
system_locale = locale.getdefaultlocale()[0]

# Set a default radar URL using station's lat/lon. Moved from skin.conf so we can get station lat/lon from weewx.conf. A lot of stations out there with Belchertown 0.1 through 0.7 are showing the visitor's location and not the proper station location because nobody edited the radar_html which did not have lat/lon set previously.
if self.generator.skin_dict['Extras']['radar_html'] == "":
lat = self.generator.config_dict['Station']['latitude']
Expand Down Expand Up @@ -679,6 +683,7 @@ def get_extension_list(self, timespan, db_lookup):
'belchertown_root_url': belchertown_root_url,
'moment_js_utc_offset': moment_js_utc_offset,
'highcharts_timezoneoffset': highcharts_timezoneoffset,
'system_locale': system_locale,
'radar_html': radar_html,
'alltime' : all_stats,
'year_outTemp_range_max': year_outTemp_range_max,
Expand Down
2 changes: 1 addition & 1 deletion skins/Belchertown/header.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js'></script>
<![endif]-->
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
#if $page == "home" and $Extras.has_key("mqtt_enabled") and $Extras.mqtt_enabled == '1'
<script type='text/javascript' src="//cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js"></script>
#end if
Expand Down
10 changes: 6 additions & 4 deletions skins/Belchertown/index.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
var mqttMsg;
var mqttclient = "website" + Math.floor(Math.random() * 999999999);
#end if
var moment_locale = "$system_locale".substring(0, 2);
moment.locale(moment_locale); // "en" or "de", etc.

jQuery(document).ready(function() {
#if $unit.unit_type.outTemp == "degree_F"
Expand Down Expand Up @@ -244,8 +246,8 @@
jQuery(".dailystatsrainrate").html( data["day"]["rain"]["max"] );

// Sunrise and Sunset
sunrise = moment(data["almanac"]["sunrise"], "HH:mm:ss A").format("h:mm A"); // Requires moment.js
sunset = moment(data["almanac"]["sunset"], "HH:mm:ss A").format("h:mm A"); // Requires moment.js
sunrise = moment(data["almanac"]["sunrise"], "HH:mm:ss A").format("h:mm A");
sunset = moment(data["almanac"]["sunset"], "HH:mm:ss A").format("h:mm A");
jQuery(".sunrise-value").html( sunrise );
jQuery(".sunset-value").html( sunset );

Expand Down Expand Up @@ -367,7 +369,7 @@
var weekday = "Today";
} else {
output_html += '<div class="col-sm-1-5 wuforecast border-left">';
var weekday = moment.unix( data["daily"]["data"][i]["time"] ).format( "ddd M/D" ); // Requires moment.js
var weekday = moment.unix( data["daily"]["data"][i]["time"] ).format( "ddd M/D" );
}
output_html += '<span id="weekday">'+weekday+'</span>';
output_html += '<br>';
Expand Down Expand Up @@ -487,7 +489,7 @@

// Updated time
epoch = parseFloat( data["dateTime"] ).toFixed(0);
updated = moment.unix(epoch).utcOffset($moment_js_utc_offset).format("MMMM D, YYYY, h:mm:ss a"); // requires moment.js
updated = moment.unix(epoch).utcOffset($moment_js_utc_offset).format("MMMM D, YYYY, h:mm:ss a");
//seconds_ago = moment(updated, "MMMM Do YYYY, h:mm:ss a").fromNow(); // "a few seconds ago || in a few seconds"
updated_text = "Connected to weather station live. Data received " + updated;
jQuery(".updated").html( updated_text );
Expand Down

0 comments on commit 145d7c1

Please sign in to comment.