diff --git a/.gitignore b/.gitignore index 3c3629e..e19aee9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +package* diff --git a/index.ts b/index.ts index a6af013..bbf8b5a 100644 --- a/index.ts +++ b/index.ts @@ -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 + } } \ No newline at end of file diff --git a/sample.ts b/sample.ts index b036097..39b1045 100644 --- a/sample.ts +++ b/sample.ts @@ -1,4 +1,4 @@ import Lingua from "."; -let lang = new Lingua() +let lang = new Lingua("./langs") -console.log(lang.get("en").missing.mention) \ No newline at end of file +console.log(lang.langs()) \ No newline at end of file