@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
135 lines • 3.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Language = void 0;
exports.newLanguage = newLanguage;
const language_1 = require("../../../domain/config/vo/language");
/**
* Language entity holds language configuration
*/
class Language {
constructor(defaultLang, configs) {
this.defaultLang = defaultLang;
this.configs = configs;
this.indices = [];
this.setIndices();
}
/**
* Get all language configurations
*/
languages() {
return Object.values(this.configs);
}
/**
* Get default language
*/
defaultLanguage() {
return this.defaultLang;
}
/**
* Check if a language is valid
*/
isLanguageValid(lang) {
return Object.prototype.hasOwnProperty.call(this.configs, lang);
}
/**
* Get other language keys (excluding default)
*/
otherLanguageKeys() {
return Object.keys(this.configs).filter(lang => lang !== this.defaultLang);
}
/**
* Get relative directory for a language
*/
getRelDir(name, langKey) {
const config = this.configs[langKey];
if (!config) {
throw new Error(`Language "${langKey}" not found`);
}
// This is a simplified version - in real implementation,
// you'd have a more complex directory mapping logic
return config.contentDir || 'content';
}
/**
* Validate language configuration
*/
validate() {
return (0, language_1.validateLanguageConfig)(this.configs, this.defaultLang);
}
/**
* Set language indices (ordered list)
*/
setIndices() {
const languages = [];
// Ensure default language is first
if (this.configs[this.defaultLang]) {
languages.push(this.defaultLang);
}
// Add remaining languages
for (const lang of Object.keys(this.configs)) {
if (lang !== this.defaultLang) {
languages.push(lang);
}
}
this.indices = languages;
}
/**
* Get language keys in order
*/
languageKeys() {
return [...this.indices];
}
/**
* Get language indexes
*/
languageIndexes() {
return this.indices.map((_, index) => index);
}
/**
* Get language index by language code
*/
getLanguageIndex(lang) {
const index = this.indices.indexOf(lang);
if (index === -1) {
throw new Error('Language not found in indices');
}
return index;
}
/**
* Get language by index
*/
getLanguageByIndex(idx) {
if (idx < 0 || idx >= this.indices.length) {
throw new Error('Language index out of range');
}
return this.indices[idx];
}
/**
* Get language name by language code
*/
getLanguageName(lang) {
const config = this.configs[lang];
return config ? config.languageName : '';
}
/**
* Get language configuration by language code
*/
getLanguageConfig(lang) {
return this.configs[lang];
}
/**
* Get all language configurations
*/
getConfigs() {
return this.configs;
}
}
exports.Language = Language;
/**
* Create a new Language instance from provider data
*/
function newLanguage(data) {
const configs = (0, language_1.decodeLanguageConfig)(data);
const defaultLang = data.defaultContentLanguage || 'en';
return new Language(defaultLang, configs);
}
//# sourceMappingURL=language.js.map