Last active
April 16, 2022 00:37
-
-
Save sandipchitale/c66ed3382701ab45583a3697e8660230 to your computer and use it in GitHub Desktop.
IAWA l10n #iawa #l10n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/naming-convention */ | |
import * as vscode from 'vscode'; | |
import * as path from 'path'; | |
import * as fs from 'fs'; | |
const BRANCHES = [ | |
'Main', | |
'rel_IA_22.2', | |
'rel_IA_21.4', | |
'rel_IA_21.2', | |
'rel_IA_20.4', | |
'rel_IA_20.2', | |
'rel_IA_16EP7', | |
'rel_IA_16EP5.1', | |
'rel_IA_16EP5', | |
'rel_IA_22.3' | |
]; | |
let currentBranch: string | undefined; | |
let currentWorkspaceFolderPath: string | undefined; | |
let projectname: string | undefined; | |
type LANGUAGE_IDS = 'de' | 'en' | 'es' | 'fr' | 'it' | 'ja' | 'nl' | 'zh'; | |
const EN_DOT_JSON_PATHS: any = { | |
'infoarchiveng': path.join('src', 'frontendng', 'src', 'languages', 'en.json'), | |
'infoarchive': path.join('src', 'frontend' , 'languages', 'en.json') // rel_IA_16EP5.1 and before | |
}; | |
const LANGUAGE_ID_DOT_JSON_PATHS: any = { | |
'infoarchiveng': path.join('..', '..', '..', 'localization', '{LANGUAGE_ID}', 'WEB-INF', 'classes', 'static', 'languages', '{LANGUAGE_ID}.json'), | |
'infoarchive': path.join('..', '..', '..', 'localization', '{LANGUAGE_ID}', 'WEB-INF', 'classes', 'static', 'languages', '{LANGUAGE_ID}.json') // rel_IA_16EP5.1 and before | |
}; | |
const SRC_FOLDER_PATHS: any = { | |
'infoarchiveng': path.join('src', 'frontendng', 'src', 'app'), | |
'infoarchive': path.join('src', 'frontend' , 'app') // rel_IA_16EP5.1 and before | |
}; | |
let language_id: LANGUAGE_IDS = 'en'; | |
let translations: any; | |
let disposables: Array<vscode.Disposable> = []; | |
export function activate(context: vscode.ExtensionContext) { | |
vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => { | |
if (event.affectsConfiguration('vscode-iawa-l10n.enabled')) { | |
const enabled = vscode.workspace.getConfiguration().get<boolean>('vscode-iawa-l10n.enabled'); | |
if (enabled) { | |
_activate(); | |
} else { | |
_deactivate(); | |
} | |
} | |
}); | |
const configuration = vscode.workspace.getConfiguration(); | |
const enabled = configuration.get<boolean>('vscode-iawa-l10n.enabled'); | |
if (enabled) { | |
_activate(); | |
} else { | |
_deactivate(); | |
} | |
} | |
async function _activate() { | |
const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0]; | |
if (workspaceFolder) { | |
currentWorkspaceFolderPath = workspaceFolder.uri.fsPath; | |
if (currentWorkspaceFolderPath) { | |
currentBranch = BRANCHES.find((branch) => currentWorkspaceFolderPath?.indexOf(`${path.sep}${branch}${path.sep}`) !== -1); | |
// On one of the IA branches | |
if (currentBranch) { | |
projectname = path.basename(currentWorkspaceFolderPath); | |
if (projectname && (projectname === 'infoarchiveng') || (projectname === 'infoarchive')) { | |
// Load register hover provider | |
disposables.push(vscode.languages.registerHoverProvider({ language: 'html', scheme: 'file' }, { | |
provideHover: hoverProvider | |
})); | |
disposables.push(vscode.languages.registerHoverProvider({ language: 'typescript', scheme: 'file' }, { | |
provideHover: hoverProvider | |
})); | |
disposables.push(vscode.languages.registerHoverProvider({ language: 'javascript', scheme: 'file' }, { | |
provideHover: hoverProvider | |
})); | |
loadTranslations(); | |
disposables.push(vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => { | |
if (event.affectsConfiguration('vscode-iawa-l10n.language_id')) { | |
loadTranslations(); | |
} | |
})); | |
} | |
} | |
} | |
} | |
} | |
async function loadTranslations() { | |
if (currentWorkspaceFolderPath && projectname && ((projectname === 'infoarchiveng') || (projectname === 'infoarchive'))) { | |
const configuration = vscode.workspace.getConfiguration(); | |
language_id = configuration.get<string>('vscode-iawa-l10n.language_id') as LANGUAGE_IDS; | |
const languageIdDotJsonRelativePath = (language_id === 'en') ? EN_DOT_JSON_PATHS[projectname] : LANGUAGE_ID_DOT_JSON_PATHS[projectname].replaceAll('{LANGUAGE_ID}', language_id); | |
if (languageIdDotJsonRelativePath) { | |
let jsonText = fs.readFileSync(path.join(currentWorkspaceFolderPath, languageIdDotJsonRelativePath), {encoding:'utf8', flag:'r'}); | |
if (jsonText.charCodeAt(0) === 65279) { | |
// Remove BOM marker if any | |
jsonText = jsonText.substring(1); | |
} | |
// Load translations | |
translations = JSON.parse(jsonText); | |
} | |
} | |
} | |
async function hoverProvider(textDocument: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) { | |
if (currentWorkspaceFolderPath && projectname && ((projectname === 'infoarchiveng') || (projectname === 'infoarchive')) && translations) { | |
if (textDocument.uri.fsPath.startsWith(path.join(currentWorkspaceFolderPath, SRC_FOLDER_PATHS[projectname]))) { | |
if (vscode.window.activeTextEditor) { | |
const wordRange = textDocument.getWordRangeAtPosition(position); | |
if (wordRange) { | |
const startLine = wordRange.start.line; | |
const characterMinusOne = wordRange.start.character - 1; | |
const charBefore = textDocument.getText(wordRange.with(wordRange.start.with(startLine, characterMinusOne), wordRange.start)); | |
const endLine = wordRange.end.line; | |
const characterPlusOne = wordRange.end.character + 1; | |
const charAfter = textDocument.getText(wordRange.with(wordRange.end, wordRange.end.with(endLine, characterPlusOne) )); | |
if ((charBefore === '"' || charBefore === '\'') && (charAfter === '"' || charAfter === '\'')) { | |
// word is surrounded by quotes | |
const word = textDocument.getText(wordRange); | |
if (word) { | |
const wordTranslation = translations[word]; | |
if (wordTranslation) { | |
return new vscode.Hover(wordTranslation); | |
} else { | |
return new vscode.Hover(`No translation found for: ${word}`); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return new vscode.Hover(''); | |
} | |
async function _deactivate() { | |
if (disposables.length > 0) { | |
disposables.forEach(disposable => disposable.dispose()); | |
disposables = []; | |
} | |
} | |
export function deactivate() { | |
_deactivate(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "vscode-iawa-l10n", | |
"displayName": "IAWA translation hovers", | |
"description": "Show translated strings as hovers in HTML and Typescript files in IAWA code.", | |
"icon": "images/icon.png", | |
"version": "0.0.5", | |
"license": "SEE LICENSE IN LICENSE", | |
"engines": { | |
"vscode": "^1.66.0" | |
}, | |
"publisher": "Sandip Chitale", | |
"author": { | |
"name": "Sandip Chitale", | |
"email": "[email protected]" | |
}, | |
"repository": "//depot/InfoArchive/IA/Main/Clients/tools/vscode-iawa-l10n", | |
"categories": [ | |
"Other" | |
], | |
"activationEvents": [ | |
"onLanguage:html", | |
"onLanguage:typescript", | |
"onLanguage:javascript" | |
], | |
"main": "./out/extension.js", | |
"contributes": { | |
"configuration": { | |
"properties": { | |
"vscode-iawa-l10n.enabled": { | |
"type": "boolean", | |
"default": "true", | |
"description": "Enable hover" | |
}, | |
"vscode-iawa-l10n.language_id": { | |
"type": "string", | |
"default": "en", | |
"enum": [ | |
"en", | |
"de", | |
"es", | |
"fr", | |
"it", | |
"ja", | |
"nl", | |
"zh" | |
], | |
"enumDescriptions": [ | |
"English", | |
"German", | |
"Spanish", | |
"French", | |
"Italian", | |
"Japanese", | |
"Dutch", | |
"Chinese" | |
], | |
"description": "Translation language" | |
} | |
} | |
} | |
}, | |
"scripts": { | |
"vscode:prepublish": "npm run compile", | |
"compile": "tsc -p ./", | |
"watch": "tsc -watch -p ./", | |
"pretest": "npm run compile && npm run lint", | |
"lint": "eslint src --ext ts", | |
"test": "node ./out/test/runTest.js" | |
}, | |
"devDependencies": { | |
"@types/vscode": "^1.66.0", | |
"@types/glob": "^7.1.4", | |
"@types/mocha": "^9.0.0", | |
"@types/node": "14.x", | |
"@typescript-eslint/eslint-plugin": "^5.1.0", | |
"@typescript-eslint/parser": "^5.1.0", | |
"eslint": "^8.1.0", | |
"glob": "^7.1.7", | |
"mocha": "^9.1.3", | |
"typescript": "^4.4.4", | |
"@vscode/test-electron": "^1.6.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment