The localization of numbers, date and time in GeoToolkit can be changed easily with the use of locale settings. By default numbers, date and time are set in "us" locale. Locales can be updated to support localized language. This Localization tutorial shows how to update locale for numbers, date and time and how to simply update locale for all of these in TickGenerator. For more information on localization please visit the Localization tutorial in the WellLogWidgets section.
# How to Update Locale
The following code shows how to update Locale for numbers and date. The external Javascript Library MomentJS is used to change date and time formats.
import {DecimalFormat} from '@int/geotoolkit/util/DecimalFormat';
// Update Locale for Date
// Use MomentDateTimeFormatFactory to generate formator
const DateFormatFactory = new MomentDateTimeFormatFactory();
const DateFormator = DateFormatFactory.createFormat({'format': 'M j Y', 'locale': 'fr', 'timezone': 'America/Chicago'});
const Date = new Date('2018-1-12');
const formattedDate = DateFormator.format(rawDate);
// Result: jan. 12 2018
// Update Locale for Number
// Setup Decimal formator
const NumberFormator = new DecimalFormat({'locale': 'ru'});
const Number = 2300.85;
const formattedNumber = NumberFormator.format(Number);
// Result: 2 300,85# Locales in TickGenerator
The AdaptiveDateTimeTickGenerator in different locales ( 'us', 'fr', 'ru', 'zh-cn' ) and the AdaptiveTickGenerator in different locales ( 'us', 'br', 'ru', 'zh-cn' ) are displayed below.
import { Orientation } from "@int/geotoolkit/util/Orientation.ts";
import { Rect } from "@int/geotoolkit/util/Rect.ts";
import { Group } from "@int/geotoolkit/scene/Group.ts";
import { DateTimeFormatFactory } from "@int/geotoolkit/util/DateTimeFormatFactory.ts";
import { Plot } from "@int/geotoolkit/plot/Plot.ts";
import { MomentDateTimeFormatFactory } from "/src/code/Carnac/Misc/Localization/momentdatetimeformatfactory.ts";
import { createAxis, createTitle } from "/src/code/Carnac/Misc/Localization/common.ts";
function createScene(canvas) {
DateTimeFormatFactory.setDefault(new MomentDateTimeFormatFactory());
const parentGroup = new Group();
const fromDate = new Date(2e3, 0, 1, 0, 0, 0, 0);
fromDate.setUTCHours(0, 0, 0, 0);
const minDate = fromDate.getTime();
const toDate = new Date(2e3, 3, 1, 0, 0, 0, 0);
toDate.setUTCHours(0, 0, 0, 0);
const maxDate = toDate.getTime();
const minValue = 5432.5;
const maxValue = 5434.5;
let bounds = new Rect(10, 30, 380, 80);
const boundHeight = bounds.getHeight() + 10;
parentGroup.addChild([
createTitle("Adaptive Date Time TickGenerator", bounds.getCenterX(), 10),
createAxis(true, minDate, maxDate, "us", void 0, Orientation.Horizontal).setBounds(bounds),
createAxis(true, minDate, maxDate, "fr", void 0, Orientation.Horizontal).setBounds(bounds.clone().setY(bounds.getY() + boundHeight)),
createAxis(true, minDate, maxDate, "ru", void 0, Orientation.Horizontal).setBounds(bounds.clone().setY(bounds.getY() + 2 * boundHeight)),
createAxis(true, minDate, maxDate, "zh-cn", void 0, Orientation.Horizontal).setBounds(bounds.clone().setY(bounds.getY() + 3 * boundHeight))
]);
bounds = new Rect(410, 30, 790, 80);
parentGroup.addChild([
createTitle("Adaptive TickGenerator", bounds.getCenterX(), 10),
createAxis(false, minValue, maxValue, "us", void 0, Orientation.Horizontal).setBounds(bounds),
createAxis(false, minValue, maxValue, "br", void 0, Orientation.Horizontal).setBounds(bounds.clone().setY(bounds.getY() + boundHeight)),
createAxis(false, minValue, maxValue, "ru", void 0, Orientation.Horizontal).setBounds(bounds.clone().setY(bounds.getY() + 2 * boundHeight)),
createAxis(false, minValue, maxValue, "ch", void 0, Orientation.Horizontal).setBounds(bounds.clone().setY(bounds.getY() + 3 * boundHeight))
]);
return new Plot({
"canvaselement": canvas,
"root": parentGroup
});
}
export { createScene };
createScene(document.querySelector('[ref="plot"]'));
# Time Zones and Daylight Saving Time in Adaptive DateTimeTickGenerator
The AdaptiveDateTimeTickGenerator in different Time Zones ("UTC", "America/Chicago", "Europe/Paris", "Europe/Moscow") is displayed below.
Daylight Saving Time ticks and labels are displayed in blue. Note that Daylight saving time 2013 in Houston began at 2:00 am Sunday, March 10.
See list for available time zones : https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
import { Axis } from "@int/geotoolkit/axis/Axis.ts";
import { from } from "@int/geotoolkit/selection/from.ts";
import { Rect } from "@int/geotoolkit/util/Rect.ts";
import { Group } from "@int/geotoolkit/scene/Group.ts";
import { DateTimeFormatFactory } from "@int/geotoolkit/util/DateTimeFormatFactory.ts";
import { Plot } from "@int/geotoolkit/plot/Plot.ts";
import { MomentDateTimeFormatFactory } from "/src/code/Carnac/Misc/Localization/momentdatetimeformatfactory.ts";
import __vite__cjsImport7_momentTimezone from "/node_modules/.vite/deps/moment-timezone.js?v=6cbbe50f"; const moment = __vite__cjsImport7_momentTimezone.__esModule ? __vite__cjsImport7_momentTimezone.default : __vite__cjsImport7_momentTimezone;
import { createAxis, createTitle } from "/src/code/Carnac/Misc/Localization/common.ts";
function createScene(canvas) {
DateTimeFormatFactory.setDefault(new MomentDateTimeFormatFactory());
const parentGroup = new Group();
const minDate = new Date(Date.UTC(2013, 2, 10, 5, 0, 0, 0)).getTime();
const maxDate = new Date(Date.UTC(2013, 2, 10, 15, 0, 0, 0)).getTime();
const bounds = new Rect(150, 30, 260, 370);
const boundWidth = bounds.getWidth() + 10;
parentGroup.addChild([
createTitle("UTC", bounds.getCenterX(), 10),
createAxis(true, minDate, maxDate, "en").setBounds(bounds),
createTitle("America/Chicago", bounds.getCenterX() + boundWidth, 10),
createAxis(true, minDate, maxDate, "en", "America/Chicago").setBounds(bounds.clone().setX(bounds.getX() + boundWidth)),
createTitle("Europe/Paris", bounds.getCenterX() + 2 * boundWidth, 10),
createAxis(true, minDate, maxDate, "fr", "Europe/Paris").setBounds(bounds.clone().setX(bounds.getX() + 2 * boundWidth)),
createTitle("Europe/Moscow", bounds.getCenterX() + 3 * boundWidth, 10),
createAxis(true, minDate, maxDate, "ru", "Europe/Moscow").setBounds(bounds.clone().setX(bounds.getX() + 3 * boundWidth))
]);
from(parentGroup).where((node) => node instanceof Axis).select((axis) => {
axis.getTickGenerator().setLabelGradeFormat("edge", "M j Y<b\\r/>H:i").setLabelGradeFormat("major", "M j H:i").setLabelGradeFormat("minor", "H:i").setVisibleLabelGrade("minor", true).setVisibleTickGrade("minor", true).setTickSize("minor", 10);
if (axis.getTickGenerator().getTimeZone() === "America/Chicago") {
const zone = moment.tz.zone("America/Chicago");
axis.getTickGenerator().setDSTTimestamps(zone.untils).setLabelGradeFormat("dst", "l<b\\r/>M j <b\\r/>H:i").setVisibleLabelGrade("dst", true).setVisibleTickGrade("dst", true).setTickSize("dst", 15);
}
});
return new Plot({
"canvaselement": canvas,
"root": parentGroup
});
}
export { createScene };
createScene(document.querySelector('[ref="plot"]'));