diff --git a/.eslintrc.js b/.eslintrc.js index b8671f8..7c05455 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -242,7 +242,7 @@ module.exports = { "no-useless-constructor": "error", "no-useless-rename": "error", "no-useless-return": "error", - "no-var": "off", + "no-var": "error", "no-void": "error", "no-warning-comments": "warn", "no-whitespace-before-property": "error", diff --git a/js/src/lang.js b/js/src/lang.js index 4dbbb9c..593b554 100644 --- a/js/src/lang.js +++ b/js/src/lang.js @@ -73,7 +73,7 @@ export default class uLang { getLocaleSpeed(ms, withUnit) { const value = Math.round(ms * this.config.factorSpeed * 100) / 100; if (withUnit) { - return `${value} ${this.unit('unitSpeed')}`; + return `${value.toLocaleString(this.config.lang)} ${this.unit('unitSpeed')}`; } return value; } @@ -87,7 +87,7 @@ export default class uLang { getLocaleDistanceMajor(m, withUnit) { const value = Math.round(m * this.config.factorDistanceMajor / 10) / 100; if (withUnit) { - return `${value} ${this.unit('unitDistanceMajor')}` + return `${value.toLocaleString(this.config.lang)} ${this.unit('unitDistanceMajor')}` } return value; } @@ -100,7 +100,7 @@ export default class uLang { getLocaleDistance(m, withUnit) { const value = Math.round(m * this.config.factorDistance * 100) / 100; if (withUnit) { - return `${value} ${this.unit('unitDistance')}`; + return `${value.toLocaleString(this.config.lang)} ${this.unit('unitDistance')}`; } return value; } @@ -141,7 +141,7 @@ export default class uLang { * @return {string} */ getLocaleCoordinates(pos) { - return `${uLang.coordStr(pos.longitude, true)} ${uLang.coordStr(pos.latitude, false)}`; + return `${this.coordStr(pos.longitude, true)} ${this.coordStr(pos.latitude, false)}`; } /** @@ -149,9 +149,9 @@ export default class uLang { * @param {boolean} isLon * @return {string} */ - static coordStr(pos, isLon) { - const ipos = Math.floor(pos); - const dec = ((pos - ipos) * 60).toFixed(1); + coordStr(pos, isLon) { + const ipos = Math.trunc(pos); + const dec = Math.abs((pos - ipos) * 60); let dir; if (isLon) { @@ -159,6 +159,6 @@ export default class uLang { } else { dir = pos < 0 ? 'S' : 'N'; } - return `${ipos}° ${dec}'${dir}`; + return `${Math.abs(ipos).toLocaleString(this.config.lang)}°${dec.toLocaleString(this.config.lang, { maximumFractionDigits: 2 })}'${dir}`; } } diff --git a/js/test/lang.test.js b/js/test/lang.test.js index 129632d..7bd576e 100644 --- a/js/test/lang.test.js +++ b/js/test/lang.test.js @@ -17,6 +17,7 @@ * along with this program; if not, see . */ +import TrackFactory from './helpers/trackfactory.js'; import uLang from '../src/lang.js'; describe('Lang tests', () => { @@ -29,6 +30,7 @@ describe('Lang tests', () => { beforeEach(() => { lang = new uLang(); mockConfig = { + lang: 'en', factorSpeed: 0.33, unitSpeed: 'units', factorDistance: 1.3, @@ -129,7 +131,7 @@ describe('Lang tests', () => { // when lang.init(mockConfig, mockStrings); // then - expect(lang.getLocaleDistance(value, true)).toBe(`1300 ${mockStrings.unitd}`); + expect(lang.getLocaleDistance(value, true)).toBe(`1,300 ${mockStrings.unitd}`); }); it('should return localized altitude value', () => { @@ -143,7 +145,7 @@ describe('Lang tests', () => { // when lang.init(mockConfig, mockStrings); // then - expect(lang.getLocaleDistance(value, true)).toBe(`1300 ${mockStrings.unitd}`); + expect(lang.getLocaleDistance(value, true)).toBe(`1,300 ${mockStrings.unitd}`); }); it('should return localized accuracy value', () => { @@ -157,7 +159,7 @@ describe('Lang tests', () => { // when lang.init(mockConfig, mockStrings); // then - expect(lang.getLocaleDistance(value, true)).toBe(`1300 ${mockStrings.unitd}`); + expect(lang.getLocaleDistance(value, true)).toBe(`1,300 ${mockStrings.unitd}`); }); it('should return localized time duration', () => { @@ -174,4 +176,41 @@ describe('Lang tests', () => { expect(lang.getLocaleDuration(123456789)).toBe(`1428 ${mockStrings.unitday} 21:33:09`); }); + it('should return localized coordinates', () => { + // when + lang.init(mockConfig, mockStrings); + const testCoord = 'coord'; + spyOn(lang, 'coordStr').and.returnValue(testCoord); + // then + expect(lang.getLocaleCoordinates(TrackFactory.getPosition())).toBe(`${testCoord} ${testCoord}`); + }); + + it('should return localized coordinate for locale "en"', () => { + // when + lang.init(mockConfig, mockStrings); + // then + expect(lang.coordStr(1.111, true)).toBe('1°6.66\'E'); + expect(lang.coordStr(1.111, false)).toBe('1°6.66\'N'); + + expect(lang.coordStr(171.11, true)).toBe('171°6.6\'E'); + expect(lang.coordStr(81.11, false)).toBe('81°6.6\'N'); + + expect(lang.coordStr(-1.111, true)).toBe('1°6.66\'W'); + expect(lang.coordStr(-1.111, false)).toBe('1°6.66\'S'); + + expect(lang.coordStr(0, true)).toBe('0°0\'E'); + expect(lang.coordStr(0, false)).toBe('0°0\'N'); + + expect(lang.coordStr(-0.01, true)).toBe('0°0.6\'W'); + expect(lang.coordStr(-0.01, false)).toBe('0°0.6\'S'); + }); + + it('should return localized coordinate for locale "pl"', () => { + // when + mockConfig.lang = 'pl'; + lang.init(mockConfig, mockStrings); + // then + expect(lang.coordStr(1.111, true)).toBe('1°6,66\'E'); + expect(lang.coordStr(1.111, false)).toBe('1°6,66\'N'); + }); });