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
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"micromatch": "4.0.2",
"normalize-path": "^3.0.0",
"ora": "^5.1.0",
"pathe": "^1.1.0",
"pkg-up": "^3.1.0",
"pofile": "^1.1.4",
"pseudolocale": "^2.0.0",
Expand Down
86 changes: 82 additions & 4 deletions packages/cli/src/api/catalog/getCatalogDependentFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ import { getCatalogDependentFiles, getFormat } from "@lingui/cli/api"
import { makeConfig } from "@lingui/conf"
import { Catalog } from "../catalog"
import { FormatterWrapper } from "../formats"
import mockFs from "mock-fs"

describe("getCatalogDependentFiles", () => {
let format: FormatterWrapper

beforeAll(async () => {
format = await getFormat("po", {}, "en")
})
afterEach(() => {
mockFs.restore()
})

it("Should return list template + fallbacks + sourceLocale", async () => {
mockFs({
"src/locales": {
"messages.pot": "bla",
"en.po": "bla",
"pl.po": "bla",
"es.po": "bla",
"pt-PT.po": "bla",
"pt-BR.po": "bla",
},
})

it("Should return list template + fallbacks + sourceLocale", () => {
const config = makeConfig(
{
locales: ["en", "pl", "es", "pt-PT", "pt-BR"],
Expand All @@ -34,7 +49,10 @@ describe("getCatalogDependentFiles", () => {
config
)

expect(getCatalogDependentFiles(catalog, "pt-PT")).toMatchInlineSnapshot(`
const actual = await getCatalogDependentFiles(catalog, "pt-PT")
mockFs.restore()

expect(actual).toMatchInlineSnapshot(`
[
src/locales/messages.pot,
src/locales/pt-BR.po,
Expand All @@ -43,7 +61,18 @@ describe("getCatalogDependentFiles", () => {
`)
})

it("Should not return itself", () => {
it("Should not return itself", async () => {
mockFs({
"src/locales": {
"messages.pot": "bla",
"en.po": "bla",
"pl.po": "bla",
"es.po": "bla",
"pt-PT.po": "bla",
"pt-BR.po": "bla",
},
})

const config = makeConfig(
{
locales: ["en", "pl", "es", "pt-PT", "pt-BR"],
Expand All @@ -67,10 +96,59 @@ describe("getCatalogDependentFiles", () => {
config
)

expect(getCatalogDependentFiles(catalog, "en")).toMatchInlineSnapshot(`
const actual = await getCatalogDependentFiles(catalog, "en")
mockFs.restore()

expect(actual).toMatchInlineSnapshot(`
[
src/locales/messages.pot,
]
`)
})

it("Should not return non-existing files", async () => {
mockFs({
"src/locales": {
// "messages.pot": "bla",
"en.po": "bla",
"pl.po": "bla",
"es.po": "bla",
"pt-PT.po": "bla",
"pt-BR.po": "bla",
},
})

const config = makeConfig(
{
locales: ["en", "pl", "es", "pt-PT", "pt-BR"],
sourceLocale: "en",
fallbackLocales: {
"pt-PT": "pt-BR",
default: "en",
},
},
{ skipValidation: true }
)

const catalog = new Catalog(
{
name: null,
path: "src/locales/{locale}",
include: ["src/"],
exclude: [],
format,
},
config
)

const actual = await getCatalogDependentFiles(catalog, "pt-PT")
mockFs.restore()

expect(actual).toMatchInlineSnapshot(`
[
src/locales/pt-BR.po,
src/locales/en.po,
]
`)
})
})
20 changes: 17 additions & 3 deletions packages/cli/src/api/catalog/getCatalogDependentFiles.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Catalog } from "../catalog"
import { getFallbackListForLocale } from "./getFallbackListForLocale"
import path from "pathe"
import fs from "node:fs/promises"

const fileExists = async (path: string) =>
!!(await fs.stat(path).catch(() => false))

/**
* Return all files catalog implicitly depends on.
*/
export function getCatalogDependentFiles(
export async function getCatalogDependentFiles(
catalog: Catalog,
locale: string
): string[] {
): Promise<string[]> {
const files = new Set([catalog.templateFile])
getFallbackListForLocale(catalog.config.fallbackLocales, locale).forEach(
(locale) => {
Expand All @@ -19,5 +24,14 @@ export function getCatalogDependentFiles(
files.add(catalog.getFilename(catalog.config.sourceLocale))
}

return Array.from(files.values())
const out: string[] = []

for (const file of files) {
const filePath = path.join(catalog.config.rootDir, file)
if (await fileExists(filePath)) {
out.push(filePath)
}
}

return out
}
5 changes: 2 additions & 3 deletions packages/loader/src/webpackLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ const loader: LoaderDefinitionFunction<LinguiLoaderOptions> = async function (
await getCatalogs(config)
)

getCatalogDependentFiles(catalog, locale).forEach((locale) => {
this.addDependency(catalog.getFilename(locale))
})
const dependency = await getCatalogDependentFiles(catalog, locale)
dependency.forEach((file) => this.addDependency(file))

const messages = await catalog.getTranslations(locale, {
fallbackLocales: config.fallbackLocales,
Expand Down
3 changes: 3 additions & 0 deletions packages/loader/test/json-format/lingui.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ export default {
path: "<rootDir>/locale/{locale}",
},
],
fallbackLocales: {
default: "en",
},
format: formatter({ style: "minimal" }),
}
3 changes: 3 additions & 0 deletions packages/loader/test/po-format/.linguirc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"catalogs": [{
"path": "<rootDir>/locale/{locale}"
}],
"fallbackLocales": {
"default": "en"
},
"format": "po"
}
5 changes: 2 additions & 3 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ Please check that catalogs.path is filled properly.\n`

const { locale, catalog } = fileCatalog

getCatalogDependentFiles(catalog, locale).forEach((locale) => {
this.addWatchFile(catalog.getFilename(locale))
})
const dependency = await getCatalogDependentFiles(catalog, locale)
dependency.forEach((file) => this.addWatchFile(file))

const messages = await catalog.getTranslations(locale, {
fallbackLocales: config.fallbackLocales,
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-plugin/test/json-format/lingui.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ export default {
path: "<rootDir>/locale/{locale}",
},
],
fallbackLocales: {
default: "en",
},
format: formatter({ style: "minimal" }),
}
3 changes: 3 additions & 0 deletions packages/vite-plugin/test/po-format/.linguirc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"catalogs": [{
"path": "<rootDir>/locale/{locale}"
}],
"fallbackLocales": {
"default": "en"
},
"format": "po"
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,7 @@ __metadata:
mockdate: ^3.0.5
normalize-path: ^3.0.0
ora: ^5.1.0
pathe: ^1.1.0
pkg-up: ^3.1.0
pofile: ^1.1.4
pseudolocale: ^2.0.0
Expand Down