Skip to content

Commit

Permalink
feat: allow to disable lineNumbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Moreno committed Mar 18, 2021
1 parent d6baa1b commit 784a567
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
4 changes: 3 additions & 1 deletion docs/ref/conf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,14 @@ Origin is filename and line number from where the message was extracted.
Note that origins may produce a large amount of merge conflicts. Origins can be
disabled by setting ``origins: false`` in :conf:`formatOptions`.

Also, you can disable just ``lineNumbers`` but keep ``origins``

.. config:: formatOptions

formatOptions
-------------

Default: ``{ origins: true }``
Default: ``{ origins: true, lineNumbers: true }``

Object for configuring message catalog output. See individual formats for options.

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type MessageType = ExtractedMessageType & {
translation: string
}

type ExtractedCatalogType = {
export type ExtractedCatalogType = {
[msgId: string]: ExtractedMessageType
}

Expand Down
59 changes: 58 additions & 1 deletion packages/cli/src/api/formats/lingui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ describe("lingui format", function () {
},
withDescription: {
translation: "Message with description",
extractedComments: ["Description is comment from developers to translators"],
extractedComments: [
"Description is comment from developers to translators",
],
},
withComments: {
comments: ["Translator comment", "This one might come from developer"],
Expand Down Expand Up @@ -122,4 +124,59 @@ describe("lingui format", function () {
const linguiOriginProperty = '"origin"'
expect(lingui).toEqual(expect.not.stringContaining(linguiOriginProperty))
})

it("should not include lineNumbers if lineNumbers option is false", function () {
mockFs({
locale: {
en: mockFs.directory(),
},
})

const filename = path.join("locale", "en", "messages.json")
const catalog: CatalogType = {
static: {
translation: "Static message",
},
withOrigin: {
translation: "Message with origin",
origin: [["src/App.js", 4]],
},
withMultipleOrigins: {
translation: "Message with multiple origin",
origin: [
["src/App.js", 4],
["src/Component.js", 2],
],
},
}
format.write(filename, catalog, { lineNumbers: false, locale: "en" })
const lingui = fs.readFileSync(filename).toString()
mockFs.restore()
expect(lingui).toMatchInlineSnapshot(`
{
"static": {
"translation": "Static message"
},
"withOrigin": {
"translation": "Message with origin",
"origin": [
[
"src/App.js"
]
]
},
"withMultipleOrigins": {
"translation": "Message with multiple origin",
"origin": [
[
"src/App.js"
],
[
"src/Component.js"
]
]
}
}
`)
})
})
15 changes: 14 additions & 1 deletion packages/cli/src/api/formats/lingui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs"
import * as R from "ramda"

import { writeFileIfChanged } from "../utils"
import { CatalogType } from "../catalog"
import { ExtractedMessageType, CatalogType } from "../catalog"
import { CatalogFormatter } from "."

type NoOriginsCatalogType = {
Expand All @@ -13,6 +13,16 @@ const removeOrigins = (R.map(
({ origin, ...message }) => message
) as unknown) as (catalog: CatalogType) => NoOriginsCatalogType

const removeLineNumbers = (R.map(
(message: ExtractedMessageType) => {
if (message.origin) {
message.origin.map(originValue => originValue.pop())
}
return message
}
) as unknown) as (catalog: ExtractedMessageType) => NoOriginsCatalogType


const lingui: CatalogFormatter = {
catalogExtension: ".json",

Expand All @@ -21,6 +31,9 @@ const lingui: CatalogFormatter = {
if (options.origins === false) {
outputCatalog = removeOrigins(catalog)
}
if (options.origins !== false && options.lineNumbers === false) {
outputCatalog = removeLineNumbers(outputCatalog)
}
writeFileIfChanged(filename, JSON.stringify(outputCatalog, null, 2))
},

Expand Down
1 change: 1 addition & 0 deletions packages/conf/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { GeneratorOptions } from "@babel/core";
export declare type CatalogFormat = "lingui" | "minimal" | "po" | "csv" | "po-gettext";
export type CatalogFormatOptions = {
origins?: boolean;
lineNumbers?: boolean;
}
export declare type OrderBy = "messageId" | "origin";
declare type CatalogConfig = {
Expand Down
1 change: 1 addition & 0 deletions packages/conf/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Object {
},
format: po,
formatOptions: Object {
lineNumbers: true,
origins: true,
},
locales: Array [
Expand Down
3 changes: 2 additions & 1 deletion packages/conf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type CatalogFormat = "lingui" | "minimal" | "po" | "csv"

export type CatalogFormatOptions = {
origins?: boolean
lineNumbers?: boolean
}

export type OrderBy = "messageId" | "origin"
Expand Down Expand Up @@ -76,7 +77,7 @@ export const defaultConfig: LinguiConfig = {
extractBabelOptions: { plugins: [], presets: [] },
fallbackLocales: {},
format: "po",
formatOptions: { origins: true },
formatOptions: { origins: true, lineNumbers: true },
locales: [],
orderBy: "messageId",
pseudoLocale: "",
Expand Down

0 comments on commit 784a567

Please sign in to comment.