add ability to list all languages

This commit is contained in:
Lio Young 2021-07-10 13:24:33 +02:00
parent eb4dea0bf6
commit d746f3fcc0
3 changed files with 29 additions and 7 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules
package*

View File

@ -1,22 +1,43 @@
import yaml from 'js-yaml'
import { readdirSync as readdir, readFileSync as readfile } from "fs";
import path from "path";
import p from "path";
import { Language } from './src/types';
export default class Lingua {
#path = './langs'
#path: string
#langs?: void | string[]
#data?: Language[] = []
constructor() {
this.#langs = readdir(path.join(__dirname, this.#path)).filter(f => f.endsWith('.yml')) ?? []
/**
* Language Utility for [Thaldrin](https://thaldr.in)
* @param {string} path The Path to your Project's `.yml` Language files
*/
constructor(path?: string) {
if (!path) this.#path = "./langs"
else this.#path = path
this.#langs = readdir(p.join(__dirname, this.#path)).filter(f => f.endsWith('.yml')) ?? []
// @ts-ignores
this.#langs.forEach(file => this.#data.push(yaml.load(readfile(path.join(__dirname, this.#path, file)))))
this.#langs.forEach(file => this.#data.push(yaml.load(readfile(p.join(__dirname, this.#path, file)))))
}
// @ts-ignore
/**
* Get a Language by its name
* @param {string} lang The Language to get. Make sure to have a ```meta.locale``` property in your Language file.
*/
get(lang: string): Language {
// @ts-ignore
let result: Language = this.#data.find(d => d.meta.locale === lang)
return result
}
/**
* Get All available Languages
*/
langs() {
// @ts-ignore
let langs = this.#langs.map(f => f.replace('.yml', ''))
return langs
}
}

View File

@ -1,4 +1,4 @@
import Lingua from ".";
let lang = new Lingua()
let lang = new Lingua("./langs")
console.log(lang.get("en").missing.mention)
console.log(lang.langs())