Bryan Housel
2018-03-22 16:51:53 -04:00
parent c08d79e488
commit 1dbf273878
12 changed files with 206 additions and 264 deletions
+20 -15
View File
@@ -1,10 +1,9 @@
// @flow
var translations = Object.create(null);
export var currentLocale = 'en';
export var textDirection = 'ltr';
export function setLocale(_: any): void {
export function setLocale(_) {
if (translations[_] !== undefined) {
currentLocale = _;
} else if (translations[_.split('-')[0]]) {
@@ -12,34 +11,40 @@ export function setLocale(_: any): void {
}
}
export function addTranslation(id: string, value: string): void {
export function addTranslation(id, value) {
translations[id] = value;
}
/**
* Given a string identifier, try to find that string in the current
* language, and return it.
* language, and return it. This function will be called recursively
* with locale `en` if a string can not be found in the requested language.
*
* @param {string} s string identifier
* @param {string} s string identifier
* @param {object?} o object of token replacements and default string
* @param {string?} loc locale to use
* @returns {string?} locale string
*/
export function t(s: string, o: any, loc?: string): string {
export function t(s, o, loc) {
loc = loc || currentLocale;
var path = s
.split('.')
.map(function (s) {
return s.replace('<TX_DOT>', '.');
})
.map(function (s) { return s.replace('<TX_DOT>', '.'); })
.reverse();
var rep = translations[loc];
while (rep !== undefined && path.length) rep = rep[path.pop()];
while (rep !== undefined && path.length) {
rep = rep[path.pop()];
}
if (rep !== undefined) {
if (o)
for (var k in o) rep = rep.replace('{' + k + '}', o[k]);
if (o) {
for (var k in o) {
rep = rep.replace('{' + k + '}', o[k]);
}
}
return rep;
}
@@ -60,9 +65,9 @@ export function t(s: string, o: any, loc?: string): string {
/**
* Given string 'ltr' or 'rtl', save that setting
*
* @param {string} s ltr or rtl
* @param {string} dir ltr or rtl
*/
export function setTextDirection(dir: string): void {
export function setTextDirection(dir) {
textDirection = dir;
}
}
+5 -13
View File
@@ -1,19 +1,11 @@
// @flow
// A per-domain session mutex backed by a cookie and dead man's
// switch. If the session crashes, the mutex will auto-release
// after 5 seconds.
// This is a type alias (https://flow.org/en/docs/types/aliases/) which allows flow to understand the object returned by utilSessionMutex in other files.
type utilSessionMutexType = {
lock: () => boolean,
unlock: () => void,
locked: () => boolean
};
// This accepts a string and returns an object that complies with utilSessionMutexType
export function utilSessionMutex(name: string): utilSessionMutexType {
export function utilSessionMutex(name) {
var mutex = {};
var intervalID: ?IntervalID; // This indicates a Maybe type - intervalId can be null so we need to use "?IntervalID", not "IntervalID"
var intervalID;
function renew() {
var expires = new Date();
@@ -21,7 +13,7 @@ export function utilSessionMutex(name: string): utilSessionMutexType {
document.cookie = name + '=1; expires=' + expires.toUTCString();
}
mutex.lock = function (): boolean {
mutex.lock = function () {
if (intervalID) return true;
var cookie = document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + name + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1');
if (cookie) return false;
@@ -30,14 +22,14 @@ export function utilSessionMutex(name: string): utilSessionMutexType {
return true;
};
mutex.unlock = function (): void {
mutex.unlock = function () {
if (!intervalID) return;
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT';
clearInterval(intervalID);
intervalID = null;
};
mutex.locked = function (): boolean {
mutex.locked = function () {
return !!intervalID;
};
+118 -119
View File
@@ -1,4 +1,3 @@
// @flow
import { t } from './locale';
import { utilDetect } from './detect';
@@ -11,31 +10,31 @@ var locale = utilDetect().locale;
* @param {Number} m area in meters
* @param {Boolean} isImperial true for U.S. customary units; false for metric
*/
export function displayLength(m: number, isImperial: boolean): string {
var d = m * (isImperial ? 3.28084 : 1),
unit;
export function displayLength(m, isImperial) {
var d = m * (isImperial ? 3.28084 : 1);
var unit;
if (isImperial) {
if (d >= 5280) {
d /= 5280;
unit = 'miles';
} else {
unit = 'feet';
}
} else {
if (d >= 1000) {
d /= 1000;
unit = 'kilometers';
} else {
unit = 'meters';
}
}
if (isImperial) {
if (d >= 5280) {
d /= 5280;
unit = 'miles';
} else {
unit = 'feet';
}
} else {
if (d >= 1000) {
d /= 1000;
unit = 'kilometers';
} else {
unit = 'meters';
}
}
return t('units.' + unit, {
quantity: d.toLocaleString(locale, {
maximumSignificantDigits: 4
})
});
return t('units.' + unit, {
quantity: d.toLocaleString(locale, {
maximumSignificantDigits: 4
})
});
}
/**
@@ -44,105 +43,105 @@ export function displayLength(m: number, isImperial: boolean): string {
* @param {Number} m2 area in square meters
* @param {Boolean} isImperial true for U.S. customary units; false for metric
*/
export function displayArea(m2: number, isImperial: boolean): string {
var d = m2 * (isImperial ? 10.7639111056 : 1),
d1, d2, area;
var unit1: string = '';
var unit2: string = '';
export function displayArea(m2, isImperial) {
var d = m2 * (isImperial ? 10.7639111056 : 1);
var d1, d2, area;
var unit1 = '';
var unit2 = '';
if (isImperial) {
if (d >= 6969600) { // > 0.25mi² show mi²
d1 = d / 27878400;
unit1 = 'square_miles';
} else {
d1 = d;
unit1 = 'square_feet';
}
if (isImperial) {
if (d >= 6969600) { // > 0.25mi² show mi²
d1 = d / 27878400;
unit1 = 'square_miles';
} else {
d1 = d;
unit1 = 'square_feet';
}
if (d > 4356 && d < 43560000) { // 0.1 - 1000 acres
d2 = d / 43560;
unit2 = 'acres';
}
if (d > 4356 && d < 43560000) { // 0.1 - 1000 acres
d2 = d / 43560;
unit2 = 'acres';
}
} else {
if (d >= 250000) { // > 0.25km² show km²
d1 = d / 1000000;
unit1 = 'square_kilometers';
} else {
d1 = d;
unit1 = 'square_meters';
}
} else {
if (d >= 250000) { // > 0.25km² show km²
d1 = d / 1000000;
unit1 = 'square_kilometers';
} else {
d1 = d;
unit1 = 'square_meters';
}
if (d > 1000 && d < 10000000) { // 0.1 - 1000 hectares
d2 = d / 10000;
unit2 = 'hectares';
}
}
if (d > 1000 && d < 10000000) { // 0.1 - 1000 hectares
d2 = d / 10000;
unit2 = 'hectares';
}
}
area = t('units.' + unit1, {
quantity: d1.toLocaleString(locale, {
maximumSignificantDigits: 4
})
});
area = t('units.' + unit1, {
quantity: d1.toLocaleString(locale, {
maximumSignificantDigits: 4
})
});
if (d2) {
return t('units.area_pair', {
area1: area,
area2: t('units.' + unit2, {
quantity: d2.toLocaleString(locale, {
maximumSignificantDigits: 2
})
})
});
} else {
return area;
}
if (d2) {
return t('units.area_pair', {
area1: area,
area2: t('units.' + unit2, {
quantity: d2.toLocaleString(locale, {
maximumSignificantDigits: 2
})
})
});
} else {
return area;
}
}
function wrap(x: number, min: number, max: number): number {
var d = max - min;
return ((x - min) % d + d) % d + min;
function wrap(x, min, max) {
var d = max - min;
return ((x - min) % d + d) % d + min;
}
function clamp(x: number, min: number, max: number): number {
return Math.max(min, Math.min(x, max));
function clamp(x, min, max) {
return Math.max(min, Math.min(x, max));
}
function displayCoordinate(deg: number, pos: any, neg: any): string {
var min = (Math.abs(deg) - Math.floor(Math.abs(deg))) * 60,
sec = (min - Math.floor(min)) * 60,
displayDegrees = t('units.arcdegrees', {
quantity: Math.floor(Math.abs(deg)).toLocaleString(locale)
}),
displayCoordinate;
function displayCoordinate(deg, pos, neg) {
var min = (Math.abs(deg) - Math.floor(Math.abs(deg))) * 60;
var sec = (min - Math.floor(min)) * 60;
var displayDegrees = t('units.arcdegrees', {
quantity: Math.floor(Math.abs(deg)).toLocaleString(locale)
});
var displayCoordinate;
if (Math.floor(sec) > 0) {
displayCoordinate = displayDegrees +
t('units.arcminutes', {
quantity: Math.floor(min).toLocaleString(locale)
}) +
t('units.arcseconds', {
quantity: Math.round(sec).toLocaleString(locale)
});
} else if (Math.floor(min) > 0) {
displayCoordinate = displayDegrees +
t('units.arcminutes', {
quantity: Math.round(min).toLocaleString(locale)
});
} else {
displayCoordinate = t('units.arcdegrees', {
quantity: Math.round(Math.abs(deg)).toLocaleString(locale)
});
}
if (Math.floor(sec) > 0) {
displayCoordinate = displayDegrees +
t('units.arcminutes', {
quantity: Math.floor(min).toLocaleString(locale)
}) +
t('units.arcseconds', {
quantity: Math.round(sec).toLocaleString(locale)
});
} else if (Math.floor(min) > 0) {
displayCoordinate = displayDegrees +
t('units.arcminutes', {
quantity: Math.round(min).toLocaleString(locale)
});
} else {
displayCoordinate = t('units.arcdegrees', {
quantity: Math.round(Math.abs(deg)).toLocaleString(locale)
});
}
if (deg === 0) {
return displayCoordinate;
} else {
return t('units.coordinate', {
coordinate: displayCoordinate,
direction: t('units.' + (deg > 0 ? pos : neg))
});
}
if (deg === 0) {
return displayCoordinate;
} else {
return t('units.coordinate', {
coordinate: displayCoordinate,
direction: t('units.' + (deg > 0 ? pos : neg))
});
}
}
/**
@@ -150,11 +149,11 @@ function displayCoordinate(deg: number, pos: any, neg: any): string {
*
* @param {Array<Number>} coord longitude and latitude
*/
export function dmsCoordinatePair(coord: number[]): string {
return t('units.coordinate_pair', {
latitude: displayCoordinate(clamp(coord[1], -90, 90), 'north', 'south'),
longitude: displayCoordinate(wrap(coord[0], -180, 180), 'east', 'west')
});
export function dmsCoordinatePair(coord) {
return t('units.coordinate_pair', {
latitude: displayCoordinate(clamp(coord[1], -90, 90), 'north', 'south'),
longitude: displayCoordinate(wrap(coord[0], -180, 180), 'east', 'west')
});
}
/**
@@ -163,9 +162,9 @@ export function dmsCoordinatePair(coord: number[]): string {
*
* @param {Array<Number>} coord longitude and latitude
*/
export function decimalCoordinatePair(coord: number[]): string {
return t('units.coordinate_pair', {
latitude: clamp(coord[1], -90, 90).toFixed(OSM_PRECISION),
longitude: wrap(coord[0], -180, 180).toFixed(OSM_PRECISION)
});
export function decimalCoordinatePair(coord) {
return t('units.coordinate_pair', {
latitude: clamp(coord[1], -90, 90).toFixed(OSM_PRECISION),
longitude: wrap(coord[0], -180, 180).toFixed(OSM_PRECISION)
});
}