Skip to content

Commit

Permalink
feat: reimplementing i18n for error messages without i18next #923 #924
Browse files Browse the repository at this point in the history
  • Loading branch information
ocruze committed Jul 3, 2024
1 parent 5a0a48f commit 8252180
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 70 deletions.
39 changes: 0 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"dependencies": {
"fast-xml-parser": "^4.2.2",
"i18next": "^23.11.5",
"geostyler-style": "^9.0.1",
"lodash": "^4.17.21"
},
Expand Down
42 changes: 17 additions & 25 deletions src/SldStyleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
} from 'fast-xml-parser';

import { merge } from 'lodash';
import i18next from 'i18next';

import {
geoStylerFunctionToSldFunction,
Expand Down Expand Up @@ -102,9 +101,9 @@ const COMBINATION_MAP = {
type CombinationType = keyof typeof COMBINATION_MAP;

export type SldStyleParserTranslationKeys = {
marksymbolizerParseFailedUnknownWellknownName?: string;
marksymbolizerParseFailedUnknownWellknownName?: (params: {wellKnownName: string}) => string;
noFilterDetected?: string;
symbolizerKindParseFailed?: string;
symbolizerKindParseFailed?: (params: {sldSymbolizerName: string}) => string;
colorMapEntriesParseFailedColorUndefined?: string;
contrastEnhancParseFailedHistoAndNormalizeMutuallyExclusive?: string;
channelSelectionParseFailedRGBAndGrayscaleMutuallyExclusive?: string;
Expand All @@ -115,10 +114,11 @@ export type SldStyleParserTranslations = Record<string, SldStyleParserTranslatio

export const defaultTranslations: SldStyleParserTranslations = {
en: {
marksymbolizerParseFailedUnknownWellknownName:
'MarkSymbolizer cannot be parsed. WellKnownName {{wellKnownName}} is not supported.',
marksymbolizerParseFailedUnknownWellknownName: ({wellKnownName}) =>
`MarkSymbolizer cannot be parsed. WellKnownName ${wellKnownName} is not supported.`,
noFilterDetected: 'No Filter detected.',
symbolizerKindParseFailed: 'Failed to parse SymbolizerKind {{sldSymbolizerName}} from SldRule.',
symbolizerKindParseFailed: ({sldSymbolizerName}) =>
`Failed to parse SymbolizerKind ${sldSymbolizerName} from SldRule.`,
colorMapEntriesParseFailedColorUndefined: 'Cannot parse ColorMapEntries. color is undefined.',
contrastEnhancParseFailedHistoAndNormalizeMutuallyExclusive:
'Cannot parse ContrastEnhancement. Histogram and Normalize are mutually exclusive.',
Expand All @@ -129,10 +129,11 @@ export const defaultTranslations: SldStyleParserTranslations = {
},
de: {},
fr: {
marksymbolizerParseFailedUnknownWellknownName:
'Échec de lecture du symbole de type MarkSymbolizer. Le WellKnownName {{wellKnownName}} n\'est pas supporté.',
marksymbolizerParseFailedUnknownWellknownName: ({wellKnownName}) =>
`Échec de lecture du symbole de type MarkSymbolizer. Le WellKnownName ${wellKnownName} n'est pas supporté.`,
noFilterDetected: 'Aucun filtre détecté.',
symbolizerKindParseFailed: 'Échec de lecture du type de symbole {{sldSymbolizerName}} à partir de SldRule.',
symbolizerKindParseFailed: ({sldSymbolizerName}) =>
`Échec de lecture du type de symbole ${sldSymbolizerName} à partir de SldRule.`,
colorMapEntriesParseFailedColorUndefined: 'Lecture de ColorMapEntries échoué. color n\'est pas défini.',
contrastEnhancParseFailedHistoAndNormalizeMutuallyExclusive:
'Échec de lecture des propriétés de contraste ContrastEnhancement échoué. '
Expand Down Expand Up @@ -276,26 +277,17 @@ export class SldStyleParser implements StyleParser<string> {
this.translations = merge(this.translations, opts.translations);
}

i18next.init({
lng: this.locale,
resources: {
en: {
translation: this.translations.en,
},
de: {
translation: this.translations.de,
},
fr: {
translation: this.translations.fr,
}
}
});

Object.assign(this, opts);
}

translate(key: keyof SldStyleParserTranslationKeys, params?: any): string {
return i18next.t(key, params) as string;
const trans = this.translations?.[this.locale]?.[key] ?? key;

if (typeof trans === 'function') {
return trans(params);
}

return trans;
}

private _parser: XMLParser;
Expand Down
12 changes: 7 additions & 5 deletions src/SldStyleParser.v1.0.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('SldStyleParser implements StyleParser (reading)', () => {

expect(readResult.errors).toBeDefined();
expect(readResult.errors?.[0].message.toString())
.contains('MarkSymbolizer cannot be parsed.');
.equals('MarkSymbolizer cannot be parsed. WellKnownName brush://dense5 is not supported.');
});
});

Expand All @@ -312,7 +312,9 @@ describe('SldStyleParser implements StyleParser (reading)', () => {

expect(readResult.errors).toBeDefined();
expect(readResult.errors?.[0].message.toString())
.contains('Échec de lecture du symbole de type MarkSymbolizer.');
.equals(
'Échec de lecture du symbole de type MarkSymbolizer. Le WellKnownName brush://dense5 n\'est pas supporté.'
);
});
});

Expand All @@ -322,8 +324,8 @@ describe('SldStyleParser implements StyleParser (reading)', () => {
locale: 'fr',
translations: {
fr: {
marksymbolizerParseFailedUnknownWellknownName:
'Echec de lecture de MarkSymbolizer. WellKnownName {{wellKnownName}} inconnu.',
marksymbolizerParseFailedUnknownWellknownName: ({ wellKnownName }) =>
`Echec de lecture de MarkSymbolizer. WellKnownName ${wellKnownName} inconnu.`,
}
}
});
Expand All @@ -333,7 +335,7 @@ describe('SldStyleParser implements StyleParser (reading)', () => {

expect(readResult.errors).toBeDefined();
expect(readResult.errors?.[0].message.toString())
.contains('Echec de lecture de MarkSymbolizer.');
.equals('Echec de lecture de MarkSymbolizer. WellKnownName brush://dense5 inconnu.');
});
});

Expand Down

0 comments on commit 8252180

Please sign in to comment.