@giancosta86/hermes
Version:
Minimalist i18n in TypeScript
48 lines • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DictionaryLibrary = void 0;
const hashed_1 = require("@rimbu/hashed");
const stream_1 = require("@rimbu/stream");
const proximity_1 = require("@rimbu/proximity");
const Dictionary_1 = require("./Dictionary");
const LocaleLike_1 = require("./LocaleLike");
class DictionaryLibrary {
static empty() {
return new DictionaryLibrary(proximity_1.ProximityMap.empty());
}
static byLanguageTag(translationsByLanguageTagThenPhrase) {
const equivalentMap = LocaleLike_1.LocaleLike.createProximityContext().from(stream_1.Stream.from(Object.entries(translationsByLanguageTagThenPhrase)).map(([languageTag, translationsByPhrase]) => [
languageTag,
hashed_1.HashMap.from(Object.entries(translationsByPhrase))
]));
return new DictionaryLibrary(equivalentMap);
}
static byPhrase(translationsByPhraseThenLanguageTag) {
const translationsByLanguageTagMapBuilder = LocaleLike_1.LocaleLike.createProximityContext().builder();
stream_1.Stream.from(Object.entries(translationsByPhraseThenLanguageTag)).forEach(([phrase, translationsByLanguageTag]) => stream_1.Stream.from(Object.entries(translationsByLanguageTag)).forEach(([languageTag, translation]) => {
let translationsForCurrentLanguageTagMapBuilder = translationsByLanguageTagMapBuilder.get(languageTag);
if (!translationsForCurrentLanguageTagMapBuilder) {
translationsForCurrentLanguageTagMapBuilder = hashed_1.HashMap.builder();
translationsByLanguageTagMapBuilder.set(languageTag, translationsForCurrentLanguageTagMapBuilder);
}
translationsForCurrentLanguageTagMapBuilder.set(phrase, translation);
}));
const translationsByLanguageTag = translationsByLanguageTagMapBuilder
.build()
.mapValues(internalBuilder => internalBuilder.build());
return new DictionaryLibrary(translationsByLanguageTag);
}
constructor(translationsByLanguageTag) {
this.translationsByLanguageTag = translationsByLanguageTag;
}
getDictionary(locale) {
const languageTag = LocaleLike_1.LocaleLike.toLanguageTag(locale);
const translations = this.translationsByLanguageTag.get(languageTag);
return new Dictionary_1.Dictionary(translations);
}
streamLanguageTags() {
return this.translationsByLanguageTag.streamKeys();
}
}
exports.DictionaryLibrary = DictionaryLibrary;
//# sourceMappingURL=DictionaryLibrary.js.map