2011-01-21 09:04:17 +00:00
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
oolite-locale-functions.js
|
|
|
|
|
|
|
|
|
|
This script implements certain functionality that may need to be modified when
|
|
|
|
|
localizing Oolite, such as number formatting. It’s intended to be overridden
|
2011-01-21 20:45:35 +00:00
|
|
|
|
by localization OXPs, and is loaded after oolite-global-prefix.js but before
|
|
|
|
|
any “normal” world scripts.
|
2011-01-21 09:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Oolite
|
2012-01-03 16:08:14 +00:00
|
|
|
|
Copyright © 2004-2012 Giles C Williams and contributors
|
2011-01-21 09:04:17 +00:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.name = "oolite-locale-functions";
|
|
|
|
|
this.author = "Jens Ayton";
|
2012-01-03 16:08:14 +00:00
|
|
|
|
this.copyright = "© 2012 the Oolite team.";
|
2012-01-03 15:14:11 +00:00
|
|
|
|
this.version = "1.77";
|
2011-01-21 09:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
|
|
// Simple parameters. For more complex behaviour change, modify the functions that do the work.
|
|
|
|
|
const minSepDigits = +expandDescription("[number-group-threshold]");
|
|
|
|
|
const groupSize = +expandDescription("[number-group-size]");
|
|
|
|
|
const maxTotalDigits = 16; // This is near the limit of numerical precision.
|
2011-02-08 22:45:30 +00:00
|
|
|
|
const maxIntegerValue = Math.pow(10, maxTotalDigits);
|
2011-01-21 09:04:17 +00:00
|
|
|
|
const radix = expandDescription("[number-decimal-separator]");
|
|
|
|
|
const groupSep = expandDescription("[number-group-separator]");
|
|
|
|
|
const currencyFormat = expandDescription("[@-credits]");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Utility to define non-enumerable, non-configurable, permanent methods, to match the behaviour of native methods.
|
|
|
|
|
// FIXME: not used because global properties are not reset when resetting game.
|
|
|
|
|
function defineMethod(object, name, implementation)
|
|
|
|
|
{
|
|
|
|
|
Object.defineProperty(object, name, { value: implementation, writable: false, configurable: false, enumerable: false });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Internal method returns a pair [result, isScientificNotation] for the benefit of format[Deci]Credits.
|
|
|
|
|
function formatPositiveIntegerInternal (value)
|
|
|
|
|
{
|
2011-02-08 22:45:30 +00:00
|
|
|
|
if (value > maxIntegerValue) return [formatScientificInternal(value), true];
|
2011-01-21 09:04:17 +00:00
|
|
|
|
var digits = value.toString();
|
|
|
|
|
var digitCount = digits.length;
|
|
|
|
|
if (digitCount >= minSepDigits)
|
|
|
|
|
{
|
|
|
|
|
if (digitCount > maxTotalDigits) return [formatScientificInternal(value), true];
|
|
|
|
|
|
|
|
|
|
var result = "";
|
|
|
|
|
var empty = true;
|
|
|
|
|
|
|
|
|
|
while (digitCount > groupSize)
|
|
|
|
|
{
|
|
|
|
|
digitCount -= groupSize;
|
|
|
|
|
|
|
|
|
|
var group = digits.substr(digitCount, groupSize);
|
|
|
|
|
if (empty)
|
|
|
|
|
{
|
|
|
|
|
result = group;
|
|
|
|
|
empty = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = group.concat(groupSep, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (digitCount > 0)
|
|
|
|
|
{
|
|
|
|
|
result = digits.substr(0, digitCount).concat(groupSep, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var result = digits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [result, false];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function formatScientificInternal(value)
|
|
|
|
|
{
|
2011-02-08 22:41:28 +00:00
|
|
|
|
return value.toString();
|
2011-01-21 09:04:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global.formatInteger = function formatInteger(value)
|
|
|
|
|
{
|
|
|
|
|
var value = Math.round(+value);
|
|
|
|
|
var negative = false;
|
|
|
|
|
if (value < 0)
|
|
|
|
|
{
|
|
|
|
|
negative = true;
|
|
|
|
|
value = -value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var string = formatPositiveIntegerInternal(value)[0];
|
|
|
|
|
|
|
|
|
|
if (negative) string = "-" + string;
|
2011-02-01 21:12:38 +00:00
|
|
|
|
return string;
|
2011-01-21 09:04:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
global.formatCredits = function formatCredits(value, includeDeciCredits, includeCurrencySymbol)
|
|
|
|
|
{
|
|
|
|
|
var negative = false;
|
|
|
|
|
if (value < 0)
|
|
|
|
|
{
|
|
|
|
|
negative = true;
|
|
|
|
|
value = -value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value += (includeDeciCredits ? 0.05 : 0.5);
|
|
|
|
|
|
|
|
|
|
var floor = Math.floor(value);
|
|
|
|
|
var [string, isSciNotation] = formatPositiveIntegerInternal(floor);
|
|
|
|
|
if (includeDeciCredits && !isSciNotation)
|
|
|
|
|
{
|
|
|
|
|
var frac = Math.floor(((value - floor) * 10));
|
|
|
|
|
string = string.concat(radix, frac.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includeCurrencySymbol)
|
|
|
|
|
{
|
|
|
|
|
string = currencyFormat.replace("%@", string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (negative) string = "-" + string;
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|