Skip to content

Commit cfa0a04

Browse files
committed
feat: display gutter icon for missing translation in locales
1 parent e4872af commit cfa0a04

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

.vscode/settings.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
"editor.defaultFormatter": "vscode.json-language-features"
1515
},
1616
"typescript.tsdk": "node_modules\\typescript\\lib",
17+
"cSpell.words": [
18+
"Unflatten",
19+
"regs"
20+
],
1721
"i18n-ally.localesPaths": ".",
1822
"i18n-ally.sourceLanguage": "en",
1923
"i18n-ally.keystyle": "flat",
2024
"i18n-ally.preferredDelimiter": "_",
2125
"i18n-ally.sortKeys": true,
22-
"cSpell.words": [
23-
"Unflatten",
24-
"regs"
25-
],
26+
"i18n-ally.keepFulfilled": true,
2627
}

src/editor/localeAnnotation.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { Global, Config, Loader, CurrentFile } from '../core'
33
import { ExtensionModule } from '../modules'
44
import { createHover } from './hover'
55

6-
const noneDecorationType = window.createTextEditorDecorationType({})
7-
86
const localeAnnotation: ExtensionModule = (ctx) => {
7+
const noneDecorationType = window.createTextEditorDecorationType({})
8+
const missingDecorationType = window.createTextEditorDecorationType({
9+
gutterIconPath: ctx.asAbsolutePath('res/dark/info.svg'),
10+
})
11+
912
const supportedParsers = Global.parsers.filter(p => p.annotationSupported)
1013

1114
function update () {
@@ -33,6 +36,8 @@ const localeAnnotation: ExtensionModule = (ctx) => {
3336

3437
const annotationDelimiter = Config.annotationDelimiter
3538
const annotations: DecorationOptions[] = []
39+
const annotationsMissing: DecorationOptions[] = []
40+
3641
const color = 'rgba(153, 153, 153, .7)'
3742
let displayLanguage = Config.displayLanguage
3843
if (displayLanguage === file.locale) {
@@ -45,41 +50,49 @@ const localeAnnotation: ExtensionModule = (ctx) => {
4550
const keys = parser.annotationGetKeys(document)
4651
// get all keys of current file
4752
keys.forEach(({ key, start, end }) => {
48-
if (Config.annotations) {
49-
let text = ''
50-
51-
if (Config.annotations && displayLanguage) {
52-
const node = loader.getTreeNodeByKey(key)
53-
if (!node || node.type === 'tree')
54-
return
55-
text = node.getValue(displayLanguage)
56-
57-
if (text) {
58-
if (maxLength && text.length > maxLength)
59-
text = `${text.substring(0, maxLength)}…`
60-
text = `${annotationDelimiter}${text}`
61-
}
62-
}
63-
64-
annotations.push({
65-
range: new Range(
66-
document.positionAt(start),
67-
document.positionAt(end),
68-
),
69-
renderOptions: {
70-
after: {
71-
color,
72-
contentText: text,
73-
fontWeight: 'normal',
74-
fontStyle: 'normal',
75-
},
53+
let sourceText = ''
54+
let currentText = ''
55+
56+
const node = loader.getTreeNodeByKey(key)
57+
const showAnnonations = Config.annotations && displayLanguage
58+
59+
if (node) {
60+
if (node.type !== 'node')
61+
return
62+
sourceText = node.getValue(displayLanguage)
63+
currentText = node.getValue(file.locale || Config.sourceLanguage)
64+
}
65+
66+
if (sourceText) {
67+
if (maxLength && sourceText.length > maxLength)
68+
sourceText = `${sourceText.substring(0, maxLength)}…`
69+
sourceText = `${annotationDelimiter}${sourceText}`
70+
}
71+
72+
const annotation: DecorationOptions = {
73+
range: new Range(
74+
document.positionAt(start),
75+
document.positionAt(end),
76+
),
77+
renderOptions: {
78+
after: {
79+
color,
80+
contentText: showAnnonations ? sourceText : undefined,
81+
fontWeight: 'normal',
82+
fontStyle: 'normal',
7683
},
77-
hoverMessage: createHover(key, maxLength),
78-
})
84+
},
85+
hoverMessage: createHover(key, maxLength),
7986
}
87+
88+
if (currentText)
89+
annotations.push(annotation)
90+
else
91+
annotationsMissing.push(annotation)
8092
})
8193

8294
activeTextEditor.setDecorations(noneDecorationType, annotations)
95+
activeTextEditor.setDecorations(missingDecorationType, annotationsMissing)
8396
}
8497

8598
const disposables: Disposable[] = []

0 commit comments

Comments
 (0)