Skip to content

Commit

Permalink
fix: add getLanguages
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhaeberle committed Apr 27, 2023
1 parent e1216a0 commit a2ec05b
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit a2ec05b

Please sign in to comment.