Skip to content

Commit

Permalink
Fix localization issues (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalyanov authored Jan 11, 2024
1 parent de10ef1 commit 0b9a7cc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v14
5 changes: 3 additions & 2 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ const languageControl = new mapgl.Control(
map,
`
<select>
<option>EN</option>
<option>RU</option>
<option>en</option>
<option>ru</option>
<option>ar</option>
</select>
`,
{
Expand Down
20 changes: 14 additions & 6 deletions src/l10n.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* @hidden
* @internal
*/
export const dictionary = {
const DEFAULT_LOCALE = 'en';

const DICTIONARY = {
start: {
en: 'Start',
ru: 'Старт',
Expand All @@ -19,4 +17,14 @@ export const dictionary = {
en: 'km',
ru: 'км',
},
};
} as const;

type L10nKey = keyof typeof DICTIONARY;

/**
* @hidden
* @internal
*/
export function getTranslation(key: L10nKey, locale: string = DEFAULT_LOCALE) {
return DICTIONARY[key][locale] ?? DICTIONARY[key][DEFAULT_LOCALE];
}
6 changes: 3 additions & 3 deletions src/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { area } from './geo/area';
import { centroid } from './geo/centroid';
import { GeoPoint } from './types';
import { style } from './style';
import { dictionary } from './l10n';
import { getTranslation } from './l10n';

/**
* @internal
Expand Down Expand Up @@ -80,10 +80,10 @@ export class Polygon {
function getLabelText(area: number, lang: string) {
lang = lang.toLowerCase();
if (area < 1e5) {
return `${area.toFixed(1)} ${dictionary.meter[lang]}²`;
return `${area.toFixed(1)} ${getTranslation('meter', lang)}²`;
}

return `${(area / 1e6).toFixed(1)} ${dictionary.kilometer[lang]}²`;
return `${(area / 1e6).toFixed(1)} ${getTranslation('kilometer', lang)}²`;
}

function createLabel(map: mapgl.Map, coordinates: GeoPoint, area: number): mapgl.Label {
Expand Down
4 changes: 2 additions & 2 deletions src/snapPoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GeoPoint, SnapInfo } from './types';
import { createHtmlMarker, getJointDistanceText, getLabelHtml, getSnapLabelHtml } from './utils';
import { style } from './style';
import { dictionary } from './l10n';
import { getTranslation } from './l10n';

/**
* @hidden
Expand Down Expand Up @@ -74,7 +74,7 @@ function createLabel(map: mapgl.Map, coordinates: GeoPoint, distance: number) {
function getLabelText(map: mapgl.Map, distance: number) {
const lang = map.getLanguage().toLowerCase();
const distanceText = getJointDistanceText(distance, false, lang);
const addJointText = dictionary.addPoint[lang] || dictionary.addPoint.en;
const addJointText = getTranslation('addPoint', lang);

return getSnapLabelHtml(distanceText, addJointText);
}
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GeoPoint, ScreenPoint, SnapInfo } from './types';
import { Joint } from './joint';
import { dictionary } from './l10n';
import { getTranslation } from './l10n';
import { style } from './style';
import { JOINT_REMOVE_BUTTON_SVG } from './constants';
import css from './index.module.css';
Expand All @@ -20,16 +20,16 @@ export function getJointDistanceText(
lang = lang.toLowerCase();

if (first) {
return dictionary.start[lang] || dictionary.start.en;
return getTranslation('start', lang);
}

if (distance < 1000) {
return `${distance} ${dictionary.meter[lang] || dictionary.meter.en}`;
return `${distance} ${getTranslation('meter', lang)}`;
}

const kmDist = (distance / 1000).toFixed(1);

return `${kmDist} ${dictionary.kilometer[lang] || dictionary.kilometer.en}`;
return `${kmDist} ${getTranslation('kilometer', lang)}`;
}

/**
Expand Down

0 comments on commit 0b9a7cc

Please sign in to comment.