Skip to content

Commit 8dd79f4

Browse files
authored
Add a check for localized strings
Asana Task URL: https://app.asana.com/0/1203301625297703/1207933241924467/f
2 parents d3684bc + 0c1199e commit 8dd79f4

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

org/allPRs.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {fail, warn, danger} from "danger"
1+
import {fail, warn, message, danger} from "danger"
22

33
export const prSize = async () => {
44
// Warn when there is a big PR
@@ -51,6 +51,27 @@ export const xcodeprojConfiguration = async () => {
5151
}
5252
}
5353

54+
export const localizedStrings = async () => {
55+
for (let file of danger.git.modified_files) {
56+
let diff = await danger.git.diffForFile(file);
57+
let addedLines = diff?.added.split(/\n/);
58+
// The regex is equal to:
59+
// * word boundary
60+
// * NSLocalizedString(
61+
// This way it will match `NSLocalizedString(` but not `NSLocalizedString` (without the opening parenthesis, which could be used in a comment).
62+
if (addedLines?.find(value => /\bNSLocalizedString\(/.test(value))) {
63+
let instructions = "";
64+
if (danger.github.thisPR.repo == "iOS") {
65+
instructions = " See [Localization Guidelines](https://app.asana.com/0/0/1185863667140706/f) for more information.";
66+
} else if (danger.github.thisPR.repo == "macos-browser") {
67+
instructions = " See [Localization Guidelines](https://app.asana.com/0/0/1206727265537758/f) for more information.";
68+
}
69+
message("You seem to be updating localized strings. Make sure that you request translations and include translated strings before you ship your change." + instructions);
70+
break;
71+
}
72+
}
73+
}
74+
5475
export const licensedFonts = async () => {
5576
// Fail if licensed fonts are committed
5677
const modifiedFiles = danger.git.modified_files;
@@ -173,6 +194,7 @@ export default async () => {
173194
await prSize()
174195
await internalLink()
175196
await xcodeprojConfiguration()
197+
await localizedStrings()
176198
await licensedFonts()
177199
await newColors()
178200
await embeddedFilesURLMismatch()
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
jest.mock("danger", () => jest.fn())
2+
import danger from 'danger'
3+
const dm = danger as any;
4+
5+
import { localizedStrings } from '../org/allPRs'
6+
7+
beforeEach(() => {
8+
dm.addedLines = ""
9+
dm.message = jest.fn().mockReturnValue(true);
10+
11+
dm.danger = {
12+
git: {
13+
diffForFile: async (_filename) => {
14+
return { added: dm.addedLines }
15+
},
16+
modified_files: [
17+
"file1.swift",
18+
"file2.swift"
19+
],
20+
},
21+
github: {
22+
pr: {
23+
additions: 200,
24+
deletions: 10
25+
},
26+
thisPR: {
27+
repo: "iOS"
28+
},
29+
},
30+
}
31+
})
32+
33+
describe("Localized Strings checks", () => {
34+
it("does not message with no changes", async () => {
35+
dm.danger.git.modified_files = []
36+
37+
await localizedStrings()
38+
39+
expect(dm.message).not.toHaveBeenCalled()
40+
})
41+
42+
it("does not message with no diff", async () => {
43+
dm.danger.git.diffForFile = async (_filename) => {}
44+
45+
await localizedStrings()
46+
47+
expect(dm.message).not.toHaveBeenCalled()
48+
})
49+
50+
it("does not message with no additions", async () => {
51+
await localizedStrings()
52+
53+
expect(dm.message).not.toHaveBeenCalled()
54+
})
55+
56+
it("does not message with added code that doesn't contain NSLocalizedString", async () => {
57+
dm.addedLines = `
58+
+ fileprivate struct Constants {
59+
+ static let databaseName = "Database"
60+
+ }
61+
+
62+
`
63+
64+
await localizedStrings()
65+
66+
expect(dm.message).not.toHaveBeenCalled()
67+
})
68+
69+
it("does not message with added code that mentions NSLocalizedString but doesn't call it", async () => {
70+
dm.addedLines = `
71+
+ // We're not using NSLocalizedString here.
72+
`
73+
74+
await localizedStrings()
75+
76+
expect(dm.message).not.toHaveBeenCalled()
77+
})
78+
79+
it("messages with added code that contains NSLocalizedString call, including iOS Localization guidelines URL when run for iOS repo", async () => {
80+
dm.danger.github.thisPR.repo = "iOS"
81+
dm.addedLines = `
82+
+ let title = NSLocalizedString("title", comment: "Title")
83+
`
84+
85+
await localizedStrings()
86+
87+
expect(dm.message).toHaveBeenCalledWith("You seem to be updating localized strings. Make sure that you request translations and include translated strings before you ship your change. See [Localization Guidelines](https://app.asana.com/0/0/1185863667140706/f) for more information.")
88+
})
89+
90+
it("messages with added code that contains NSLocalizedString call, including macOS Localization guidelines URL when run for macos-browser repo", async () => {
91+
dm.danger.github.thisPR.repo = "macos-browser"
92+
dm.addedLines = `
93+
+ let title = NSLocalizedString("title", comment: "Title")
94+
`
95+
96+
await localizedStrings()
97+
98+
expect(dm.message).toHaveBeenCalledWith("You seem to be updating localized strings. Make sure that you request translations and include translated strings before you ship your change. See [Localization Guidelines](https://app.asana.com/0/0/1206727265537758/f) for more information.")
99+
})
100+
101+
it("messages with UserText.swift-style added code, including iOS Localization guidelines URL when run for iOS repo", async () => {
102+
dm.danger.github.thisPR.repo = "iOS"
103+
dm.addedLines = `
104+
+ static let mainMenuAppCheckforUpdates = NSLocalizedString("main-menu.app.check-for-updates", value: "Check for Updates!", comment: "Main Menu DuckDuckGo item")
105+
`
106+
107+
await localizedStrings()
108+
109+
expect(dm.message).toHaveBeenCalledWith("You seem to be updating localized strings. Make sure that you request translations and include translated strings before you ship your change. See [Localization Guidelines](https://app.asana.com/0/0/1185863667140706/f) for more information.")
110+
})
111+
112+
it("messages with UserText.swift-style added code, including macOS Localization guidelines URL when run for macos-browser repo", async () => {
113+
dm.danger.github.thisPR.repo = "macos-browser"
114+
dm.addedLines = `
115+
+ static let mainMenuAppCheckforUpdates = NSLocalizedString("main-menu.app.check-for-updates", value: "Check for Updates!", comment: "Main Menu DuckDuckGo item")
116+
`
117+
118+
await localizedStrings()
119+
120+
expect(dm.message).toHaveBeenCalledWith("You seem to be updating localized strings. Make sure that you request translations and include translated strings before you ship your change. See [Localization Guidelines](https://app.asana.com/0/0/1206727265537758/f) for more information.")
121+
})
122+
})

0 commit comments

Comments
 (0)