mirror of https://github.com/thaldrin/lingua.git
add ability to list all languages
This commit is contained in:
parent
eb4dea0bf6
commit
d746f3fcc0
|
@ -1 +1,2 @@
|
|||
node_modules
|
||||
package*
|
||||
|
|
31
index.ts
31
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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue