Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make more strings localisable #446

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/timeline/adjustment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Range, Selection, TextEditor, window } from 'vscode'
import * as nls from 'vscode-nls'

import { adjustTime, localize } from '../utils'
import { adjustTime } from '../utils'

const localize = nls.loadMessageBundle()

/** indicate the whole lines is selected */
const selectWholeLines = (selection: Selection, editor: TextEditor): Range => {
Expand Down
25 changes: 18 additions & 7 deletions src/timeline/code-lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { exec } from 'child_process'
import { promisify } from 'util'

import * as vscode from 'vscode'
import * as nls from 'vscode-nls'

import { localize } from '../utils'
import { output } from '../utils'

const localize = nls.loadMessageBundle()

export class TimelineCodeLensProvider implements vscode.CodeLensProvider {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -28,16 +31,19 @@ export class TimelineCodeLensProvider implements vscode.CodeLensProvider {
codeLenses.push(
new vscode.CodeLens(range, {
command: 'cactbot.timeline.runGenerateScript',
title: 'run make_timeline',
tooltip: localize('codeLens.makeTimeline.run', 'Run `make_timeline` command'),
title: localize('codeLens.makeTimeline.run.title', 'run make_timeline'),
tooltip: localize('codeLens.makeTimeline.run.tooltip', 'Run `make_timeline` command'),
arguments: args,
}),
)
codeLenses.push(
new vscode.CodeLens(range, {
command: 'cactbot.timeline.runGenerateScriptWithoutExecution',
title: 'send make_timeline',
tooltip: localize('codeLens.makeTimeline.send', 'Send `make_timeline` command to terminal without execution'),
title: localize('codeLens.makeTimeline.send.title', 'send make_timeline'),
tooltip: localize(
'codeLens.makeTimeline.send.tooltip',
'Send `make_timeline` command to terminal without execution',
),
arguments: args,
}),
)
Expand All @@ -63,7 +69,13 @@ export async function runMakeTimeline(args: IArguments[], run = true): Promise<v

const _command = vscode.workspace.getConfiguration().get('cactbot.timeline.makeTimelineCommandTemplate') as string
if (!_command) {
vscode.window.showErrorMessage('cactbot.timeline.makeTimelineCommandTemplate is not set')
vscode.window.showErrorMessage(
localize(
'codeLens.makeTimeline.CommandTemplateNotSet',
'{0} is not set',
'cactbot.timeline.makeTimelineCommandTemplate',
),
)
return
}

Expand All @@ -82,7 +94,6 @@ export async function runMakeTimeline(args: IArguments[], run = true): Promise<v
} catch (e) {
vscode.window.showErrorMessage((e as Error).message)
// show error in output panel
const output = vscode.window.createOutputChannel('Cactbot Timeline')
output.appendLine(localize('codeLens.makeTimeline.error', 'Errors occurred while running make_timeline:'))
output.appendLine((e as Error).message)
output.appendLine('')
Expand Down
50 changes: 28 additions & 22 deletions src/timeline/translate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/* eslint-disable import/no-named-as-default-member */
import { access, constants, readFile } from 'fs/promises'

import * as ts from 'typescript'
import ts from 'typescript'
import { EventEmitter, languages, TextDocumentContentProvider, Uri, window, workspace } from 'vscode'
import * as nls from 'vscode-nls'

import { commonReplacement } from 'cactbot/ui/raidboss/common_replacement'

import { localize } from '../utils'
import { output } from '../utils'

import type { Lang } from 'cactbot/resources/languages'
import type { LocaleText } from 'cactbot/types/trigger'
import type { TimelineReplacement } from 'cactbot/ui/raidboss/timeline_parser'

const localize = nls.loadMessageBundle()

type CommonReplacement = typeof commonReplacement

const extractReplacements = async (triggerPath: string): Promise<TimelineReplacement[]> => {
Expand Down Expand Up @@ -104,7 +108,7 @@ export class TranslatedTimelineProvider implements TextDocumentContentProvider {
}
}

async provideTextDocumentContent(uri: Uri): Promise<string> {
async provideTextDocumentContent(uri: Uri) {
const timelineFilePath = uri.path
const triggerFilePath = await this.getTriggerFilePath(timelineFilePath)
if (!triggerFilePath) {
Expand All @@ -123,14 +127,26 @@ export class TranslatedTimelineProvider implements TextDocumentContentProvider {
})
} catch (e) {
const err = e as Error
return localize(
'error.timeline.translate.stack',
'Error when translating file "{0}":\n{1}\n{2}\n{3}',
uri.path,
err.name,
err.message,
err.stack,
const ans = await window.showErrorMessage(
localize(
'error.timeline.translate.dialog',
'Error when translating file "{0}": {1}\nShow more details?',
uri.path,
err.name,
),
{ modal: true },
localize('error.timeline.translate.dialog.buttons.yes', 'Yes'),
localize('error.timeline.translate.dialog.buttons.no', 'No'),
)

output.appendLine(
localize('error.timeline.translate.stack', '{0}: {1}\n{2}{3}', uri.path, err.name, err.message, err.stack),
)

if (ans === localize('error.timeline.translate.dialog.buttons.yes', 'Yes')) {
// switch to output panel
output.show()
}
}
}

Expand Down Expand Up @@ -221,9 +237,9 @@ export class TranslatedTimelineProvider implements TextDocumentContentProvider {
}
} catch (err) {
const error = err as Error
console.warn(
throw new Error(
localize(
'error.transle.line.stack',
'error.translate.line.stack',
'Error in translating line {0}:\n{1}\n{2}\n{3}',
index,
error.name,
Expand Down Expand Up @@ -285,16 +301,6 @@ export const translateTimeline = async (): Promise<void> => {
return
}
let filename = document.fileName
if (!/\w*ui.raidboss.data.\d\d.*\.(ts|js|txt)/.test(filename)) {
await window.showErrorMessage(
localize(
'error.timeline.file.not.valid',
'{0} is not a valid file path, please make sure the path of your active file is "ui/raidboss/data/**/*.(js|ts)"',
filename,
),
)
return
}

// TODO: very hacky way
// maybe it should be the `timelineFile` or `timeline` key in the trigger file
Expand Down
3 changes: 0 additions & 3 deletions src/utils/i18n.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './timeline'
export * from './i18n'
export * from './vscode'
4 changes: 4 additions & 0 deletions src/utils/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as vscode from 'vscode'

export const output = vscode.window.createOutputChannel('Cactbot Timeline')

Loading