Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit ba9e29f

Browse files
committed
feat: add linguist support
Fix #282
1 parent adf7eb1 commit ba9e29f

File tree

12 files changed

+118
-14
lines changed

12 files changed

+118
-14
lines changed

package.json

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,14 @@
130130
"category": "Qt for Python"
131131
},
132132
{
133-
"command": "qtForPython.lupdate",
133+
"command": "qtForPython.extractTranslations",
134134
"title": "Extract to Qt Translation File (lupdate)",
135135
"category": "Qt for Python"
136+
},
137+
{
138+
"command": "qtForPython.editTranslations",
139+
"title": "Edit Qt Translation File (linguist)",
140+
"category": "Qt for Python"
136141
}
137142
],
138143
"menus": {
@@ -163,9 +168,14 @@
163168
"group": "qtForPython"
164169
},
165170
{
166-
"command": "qtForPython.lupdate",
171+
"command": "qtForPython.extractTranslations",
167172
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
168173
"group": "qtForPython"
174+
},
175+
{
176+
"command": "qtForPython.editTranslations",
177+
"when": "resourceExtname == .ts && resourceLangId == xml",
178+
"group": "qtForPython"
169179
}
170180
],
171181
"explorer/context": [
@@ -195,9 +205,14 @@
195205
"group": "qtForPython"
196206
},
197207
{
198-
"command": "qtForPython.lupdate",
208+
"command": "qtForPython.extractTranslations",
199209
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
200210
"group": "qtForPython"
211+
},
212+
{
213+
"command": "qtForPython.editTranslations",
214+
"when": "resourceExtname == .ts && resourceLangId == xml",
215+
"group": "qtForPython"
201216
}
202217
],
203218
"editor/title": [
@@ -227,9 +242,14 @@
227242
"group": "qtForPython"
228243
},
229244
{
230-
"command": "qtForPython.lupdate",
245+
"command": "qtForPython.extractTranslations",
231246
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
232247
"group": "qtForPython"
248+
},
249+
{
250+
"command": "qtForPython.editTranslations",
251+
"when": "resourceExtname == .ts && resourceLangId == xml",
252+
"group": "qtForPython"
233253
}
234254
],
235255
"editor/context": [
@@ -259,9 +279,14 @@
259279
"group": "qtForPython"
260280
},
261281
{
262-
"command": "qtForPython.lupdate",
282+
"command": "qtForPython.extractTranslations",
263283
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
264284
"group": "qtForPython"
285+
},
286+
{
287+
"command": "qtForPython.editTranslations",
288+
"when": "resourceExtname == .ts && resourceLangId == xml",
289+
"group": "qtForPython"
265290
}
266291
]
267292
},

python/.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ omit =
77
scripts/designer.py
88
scripts/qml.py
99
scripts/lupdate.py
10+
scripts/linguist.py
1011

1112
[report]
1213
fail_under = 100

python/scripts/linguist.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# pylint: disable=import-error,ungrouped-imports
2+
3+
import sys
4+
5+
from utils import is_installed, parse_qt_dependency
6+
7+
if __name__ == "__main__":
8+
dep = parse_qt_dependency()
9+
if dep == "PySide6":
10+
from PySide6.scripts.pyside_tool import linguist
11+
12+
elif is_installed("PySide6"):
13+
from PySide6.scripts.pyside_tool import linguist
14+
else:
15+
sys.exit("No Qt Linguist can be found in current Python environment.")
16+
17+
sys.exit(linguist())

python/scripts/qmlls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pylint: disable=import-error
1+
# pylint: disable=import-error,ungrouped-imports
22

33
import sys
44

python/tests/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
ASSETS_DIR = os.path.join(TESTS_DIR, "assets")
1313

14-
SupportedScripts = typing.Literal["designer", "lupdate", "qml", "qmlls", "rcc", "uic"]
14+
SupportedScripts = typing.Literal[
15+
"designer", "qml", "qmlls", "rcc", "uic", "lupdate", "linguist"
16+
]
1517

1618

1719
def filter_available_qt_dependencies(

python/tests/test_linguist.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from scripts.utils import SupportedQtDependencies
4+
from tests import filter_available_qt_dependencies, invoke_script
5+
6+
7+
@pytest.mark.skip(reason="a GUI app cannot be closed gracefully")
8+
@pytest.mark.parametrize(
9+
"qt_dependency",
10+
filter_available_qt_dependencies(["PySide6"]),
11+
)
12+
def test_qml_help(qt_dependency: SupportedQtDependencies):
13+
result = invoke_script("linguist", [], qt_dependency)
14+
assert result.returncode == 0

src/commands.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { URI } from 'vscode-uri'
55
import { EXTENSION_NAMESPACE } from './constants'
66
import { createUi } from './designer/create-ui'
77
import { editUi } from './designer/edit-ui'
8-
import { lupdate } from './lupdate/lupdate'
8+
import { editTranslations } from './linguist/edit-translations'
9+
import { extractTranslations } from './lupdate/extract-translation'
910
import { previewQml } from './qml/preview-qml'
1011
import { compileResource } from './rcc/compile-resource'
1112
import type { ErrorResult, SuccessResult } from './types'
@@ -53,8 +54,12 @@ const COMMANDS = [
5354
callback: previewQml,
5455
},
5556
{
56-
name: 'lupdate',
57-
callback: lupdate,
57+
name: 'extractTranslations',
58+
callback: extractTranslations,
59+
},
60+
{
61+
name: 'editTranslations',
62+
callback: editTranslations,
5863
},
5964
] as const
6065

src/linguist/edit-translations.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { firstValueFrom } from 'rxjs'
2+
import type { CommandDeps } from '../commands'
3+
import { getTargetDocumentUri } from '../commands'
4+
import { run } from '../run'
5+
import { getToolCommand$ } from '../tool-utils'
6+
7+
export async function editTranslations(
8+
{ extensionUri }: CommandDeps,
9+
...args: any[]
10+
) {
11+
const targetDocumentUriResult = getTargetDocumentUri(...args)
12+
13+
if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult
14+
15+
const translationFile = targetDocumentUriResult.value
16+
17+
const getToolCommandResult = await firstValueFrom(
18+
getToolCommand$({
19+
tool: 'linguist',
20+
extensionUri,
21+
resource: translationFile,
22+
}),
23+
)
24+
25+
if (getToolCommandResult.kind !== 'Success') return getToolCommandResult
26+
27+
return run({
28+
command: [
29+
...getToolCommandResult.value.command,
30+
...getToolCommandResult.value.options,
31+
translationFile.fsPath,
32+
],
33+
})
34+
}

src/lupdate/lupdate.ts renamed to src/lupdate/extract-translation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { getTargetDocumentUri } from '../commands'
44
import { run } from '../run'
55
import { getToolCommand$ } from '../tool-utils'
66

7-
export async function lupdate({ extensionUri }: CommandDeps, ...args: any[]) {
7+
export async function extractTranslations(
8+
{ extensionUri }: CommandDeps,
9+
...args: any[]
10+
) {
811
const targetDocumentUriResult = getTargetDocumentUri(...args)
912

1013
if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult

src/test/suite/linguist/e2e.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ suite('linguist/e2e', () => {
4343
await removeGeneratedFile(sampleFilenameNoExt)
4444
})
4545

46-
test('should run lupdate command', async () => {
47-
await commands.executeCommand(`${EXTENSION_NAMESPACE}.lupdate`)
46+
test('should run extractTranslations command', async () => {
47+
await commands.executeCommand(
48+
`${EXTENSION_NAMESPACE}.extractTranslations`,
49+
)
4850

4951
return waitFor(async () => {
5052
const readResult = await workspace.fs.readFile(

0 commit comments

Comments
 (0)