Skip to content

Commit

Permalink
feat: allow to disable lineNumbers (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal committed Mar 19, 2021
1 parent ebaa87d commit fe67e0f
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 17 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
81 changes: 71 additions & 10 deletions packages/cli/src/api/formats/po.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import mockFs from "mock-fs"
import mockDate from "mockdate"
import PO from "pofile"
import { mockConsole } from "@lingui/jest-mocks"
import { format as formatDate } from "date-fns"

import format from "./po"
import { CatalogType } from "../catalog"

describe("pofile format", function () {
describe("pofile format", () => {
afterEach(() => {
mockFs.restore()
mockDate.reset()
})

it("should write catalog in pofile format", function () {
it("should write catalog in pofile format", () => {
mockFs({
locale: {
en: mockFs.directory(),
},
})
mockDate.set(new Date(2018,7,27,10,0,0).toUTCString())
mockDate.set(new Date(2018, 7, 27, 10, 0, 0).toUTCString())

const filename = path.join("locale", "en", "messages.po")
const catalog: CatalogType = {
Expand All @@ -40,7 +41,9 @@ describe("pofile 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 @@ -73,7 +76,7 @@ describe("pofile format", function () {
expect(pofile).toMatchSnapshot()
})

it("should read catalog in pofile format", function () {
it("should read catalog in pofile format", () => {
const pofile = fs
.readFileSync(
path.join(path.resolve(__dirname), "fixtures", "messages.po")
Expand All @@ -94,7 +97,7 @@ describe("pofile format", function () {
expect(actual).toMatchSnapshot()
})

it("should correct badly used comments", function () {
it("should correct badly used comments", () => {
const po = PO.parse(`
#. First description
#. Second comment
Expand Down Expand Up @@ -123,7 +126,7 @@ describe("pofile format", function () {
expect(actual).toMatchSnapshot()
})

it("should throw away additional msgstr if present", function () {
it("should throw away additional msgstr if present", () => {
const po = PO.parse(`
msgid "withMultipleTranslation"
msgstr[0] "This is just fine"
Expand All @@ -150,7 +153,7 @@ describe("pofile format", function () {
})
})

it("should write the same catalog as it was read", function () {
it("should write the same catalog as it was read", () => {
const pofile = fs
.readFileSync(
path.join(path.resolve(__dirname), "fixtures", "messages.po")
Expand All @@ -172,10 +175,12 @@ describe("pofile format", function () {
mockFs.restore()
// on windows mockFs adds ··· to multiline string, so this strictly equal comparison can't be done
// we test that the content if the same inlined...
expect(actual.replace(/(\r\n|\n|\r)/gm,"")).toEqual(pofile.replace(/(\r\n|\n|\r)/gm,""))
expect(actual.replace(/(\r\n|\n|\r)/gm, "")).toEqual(
pofile.replace(/(\r\n|\n|\r)/gm, "")
)
})

it("should not include origins if origins option is false", function () {
it("should not include origins if origins option is false", () => {
mockFs({
locale: {
en: mockFs.directory(),
Expand Down Expand Up @@ -205,4 +210,60 @@ describe("pofile format", function () {
const pofileOriginPrefix = "#:"
expect(pofile).toEqual(expect.not.stringContaining(pofileOriginPrefix))
})

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

const filename = path.join("locale", "en", "messages.po")
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, {
origins: true,
lineNumbers: false,
locale: "en",
})
const pofile = fs.readFileSync(filename).toString()
mockFs.restore()
expect(pofile).toMatchInlineSnapshot(`
msgid ""
msgstr ""
"POT-Creation-Date: ${formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx")}\\n"
"Mime-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"X-Generator: @lingui/cli\\n"
"Language: en\\n"
msgid "static"
msgstr "Static message"
#: src/App.js
msgid "withOrigin"
msgstr "Message with origin"
#: src/App.js
#: src/Component.js
msgid "withMultipleOrigins"
msgstr "Message with multiple origin"
`)
})
})
11 changes: 9 additions & 2 deletions packages/cli/src/api/formats/po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ const serialize = (items: CatalogType, options) =>
item.msgstr = [message.translation]
item.comments = message.comments || []
item.extractedComments = message.extractedComments || []
if (options.origins) {
item.references = message.origin ? message.origin.map(joinOrigin) : []
if (options.origins !== false) {
if (message.origin && options.lineNumbers === false) {
item.references = message.origin.map(msg => {
msg.pop()
return msg
}).map(joinOrigin)
} else {
item.references = message.origin ? message.origin.map(joinOrigin) : []
}
}
// @ts-ignore: Figure out how to set this flag
item.obsolete = message.obsolete
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

1 comment on commit fe67e0f

@vercel
Copy link

@vercel vercel bot commented on fe67e0f Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.