Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib/marshal/translation/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ export const isValidLocale = (localeCode: string): boolean =>

/*
* Evaluates whether the given directory path is a translations directory
* by checking if the directory name is a valid locale name
* by checking that the directory name is a valid locale name AND
* the parent directory is named "translations".
*/
export const isTranslationDir = (dirPath: string): boolean => {
const locale = path.basename(dirPath);
return isValidLocale(locale);
const parentDirName = path.basename(path.dirname(dirPath));
return parentDirName === "translations" && isValidLocale(locale);
};

/*
Expand Down
6 changes: 2 additions & 4 deletions src/lib/marshal/translation/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { formatErrors, SourceError } from "@/lib/helpers/error";
import { readJson } from "@/lib/helpers/json";

import {
isTranslationDir,
isValidLocale,
lsTranslationDir,
parseTranslationRef,
SYSTEM_NAMESPACE,
Expand Down Expand Up @@ -158,9 +158,7 @@ export const readAllForCommandTarget = async (
});

const translationDirPaths = dirents
.filter(
(dirent) => dirent.isDirectory() && isTranslationDir(dirent.name),
)
.filter((dirent) => dirent.isDirectory() && isValidLocale(dirent.name))
.map((dirent) => path.resolve(targetCtx.abspath, dirent.name));

const translationFilePaths = (
Expand Down
3 changes: 0 additions & 3 deletions src/lib/run-context/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ const evaluateRecursively = async (
} else if (await MessageType.isMessageTypeDir(currDir)) {
ctx.resourceDir = buildResourceDirContext("message_type", currDir);
} else if (Translation.isTranslationDir(currDir)) {
// NOTE: Must keep this check as last in the order of directory-type checks
// since the `isTranslationDir` only checks that the directory name is a
// valid locale name.
ctx.resourceDir = buildResourceDirContext("translation", currDir);
}
}
Expand Down
19 changes: 17 additions & 2 deletions test/lib/marshal/translation/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ import { isTranslationDir } from "@/lib/marshal/translation";

describe("lib/marshal/translation/helpers", () => {
describe("isTranslationDir", () => {
describe("given a path with a valid locale code", () => {
describe("given a path with a valid locale code under a translations parent", () => {
it("returns true", () => {
const path = xpath("/translations/fr-FR");
expect(isTranslationDir(path)).to.be.true;
});
});
describe("given a path with an invalid locale code", () => {

describe("given a path with a valid locale code under a non-translations parent", () => {
it("returns false", () => {
const path = xpath("/Users/mr/my-project");
expect(isTranslationDir(path)).to.be.false;
});
});

describe("given a locale-code directory name as a username in the path", () => {
it("returns false", () => {
const path = xpath("/home/mr");
expect(isTranslationDir(path)).to.be.false;
});
});

describe("given an invalid locale code under a translations parent", () => {
it("returns false", () => {
const path = xpath("/translations/fake-lang");
expect(isTranslationDir(path)).to.be.false;
Expand Down
Loading