diff --git a/src/index.ts b/src/index.ts index f249461..cad2838 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,37 @@ export type PluginConfig = { pathPattern: string; }; +/** + * Automatically derives the languages in this repository. + */ +export async function getLanguages( + args: EnvironmentFunctions & { + pluginConfig: PluginConfig; + referenceLanguage: string; + } +) { + // replace the path + const [pathBeforeLanguage, pathAfterLanguage] = + args.pluginConfig.pathPattern.split("{language}"); + + // prepared for different folder structure e.g. example/language/translation.json + // see plugin.po + const pathAfterLanguageIsDirectory = pathAfterLanguage.startsWith("/"); + + const paths = await args.$fs.readdir(pathBeforeLanguage); + // files that end with .json + const languages = []; + + for (const language of paths) { + // remove the .yml or .yaml extension to only get language name + if (typeof language === "string" && (language.endsWith(".yml") || language.endsWith(".yaml"))) { + languages.push(language.replace(/\.ya?ml$/, "")); + } + } + + return languages; +} + /** * Reading resources. *