-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmessages.ts
157 lines (151 loc) · 6.15 KB
/
messages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import axe from 'axe-core';
import fs from 'fs';
import { createRequire } from 'node:module';
import path from 'path';
import {
RESULT_TYPES_FULL_SET,
DEFAULT_LOCALE,
PACKAGE_NAME,
} from './constants.js';
interface I18nMessage {
[key: string]: LocalizedMessageTypes;
}
interface LocalizedMessageTypes {
text: LocalizedMessageString;
func: LocalizedMessageFunc;
}
interface LocalizedMessageString {
[key: string]: string;
}
interface LocalizedMessageFunc {
[key: string]: MessageFunction;
}
type MessageFunction = (...placeholderTexts: string[]) => string;
const MESSAGES: I18nMessage = {
// Messages that take required params are stored
// in the func object of the respective locales.
en: {
text: {
INFO_CURRENT_CONFIG:
'The current axe-scan configurations are as follows:\n\n',
INFO_CURRENT_CONFIG_SUFFIX:
'\nUse the "--change-value" option to modify a particular item, or directly edit the config file.',
INFO_CONFIG_UPDATED: 'Updated axe-scan configuration.',
INFO_RUNNING: 'Running accessibility scan...',
INFO_SUMMARY: 'Creating summarized report of the accessibility scan...',
ERROR_INIT_ABORT: `No overwriting. Aborting ${PACKAGE_NAME} initiation process.`,
ERROR_INVALID_VALUE_FOR_RESULTTYPES: `Invalid value for key "resultTypes". Choose one or more of the following: ${RESULT_TYPES_FULL_SET.join(
', '
)}.`,
ERROR_KEY_VALUE_NOT_SET:
'<key>=<value> must be set when using the --change-value option.\ne.g. axe-scan config --change-value locale=ja\ne.g. axe-scan config -V resultTypes=violation,incomplete,inapplicable',
PROMPT_CONFIG_FILE_ALREADY_EXISTS:
'Configuration file already exists in the directory. Do you want to overwrite? (y/N)',
},
func: {
INFO_INITIATION_COMPLETE_CREATED: (
targetConfigFilePath: string
): string => `${targetConfigFilePath} has been created.`,
INFO_INITIATION_COMPLETE_UPDATED: (
targetConfigFilePath: string
): string => `${targetConfigFilePath} has been updated.`,
ERROR_CONFIG_FILE_NOT_FOUND: (
targetConfigFilePath: string,
globalFlag: string
): string =>
`${targetConfigFilePath} does not exist. Try running "axe-scan init${globalFlag}" first.`,
ERROR_INVALID_CONFIG_KEY: (key: string) =>
`${key} is not a valid axe-scan configuration item.`,
},
},
ja: {
text: {
INFO_CURRENT_CONFIG: '現在のaxe-scan設定は次のとおりです:\n\n',
INFO_CURRENT_CONFIG_SUFFIX:
'\n設定を変更する場合はオプション --change-value を使うか、直接設定ファイルを編集してください。',
INFO_CONFIG_UPDATED: 'axe-scanの設定が更新されました。',
INFO_RUNNING: 'アクセシビリティ検査を実行中...',
INFO_SUMMARY: 'アクセシビリティ検査報告書を作成中...',
ERROR_INIT_ABORT: `中断:${PACKAGE_NAME} 初期化を中断しました。設定ファイルは上書きされませんでした。`,
ERROR_INVALID_VALUE_FOR_RESULTTYPES: `resultTypes の値として無効なものが含まれています。次の中から1つ以上を指定してください: ${RESULT_TYPES_FULL_SET.join(
', '
)}`,
ERROR_KEY_VALUE_NOT_SET:
'オプション --change-value を使用するときは、併せて <key>=<value> を指定する必要があります。\n例) axe-scan config --change-value locale=ja\n例) axe-scan config -V resultTypes=violation,incomplete,inapplicable',
PROMPT_CONFIG_FILE_ALREADY_EXISTS:
'設定ファイルはディレクトリ内にすでに存在します。上書きますか? (y/N)',
},
func: {
INFO_INITIATION_COMPLETE_CREATED: (
targetConfigFilePath: string
): string => `完了:設定ファイル ${targetConfigFilePath} 新規作成`,
INFO_INITIATION_COMPLETE_UPDATED: (
targetConfigFilePath: string
): string => `完了:設定ファイル ${targetConfigFilePath} 更新`,
ERROR_CONFIG_FILE_NOT_FOUND: (
targetConfigFilePath: string,
globalFlag: string
): string =>
`${targetConfigFilePath} が存在しません。先に "axe-scan init${globalFlag}" を実行してください。`,
ERROR_INVALID_CONFIG_KEY: (key: string) =>
`${key} はaxe-scanの設定項目ではありません。`,
},
},
};
export class MessageLocalization {
locale: string;
message: LocalizedMessageTypes;
constructor(locale: string) {
if (locale.match(/^[A-Za-z]{2}(_[A-Za_z]{2})?$/)) {
const availableLocales = Object.keys(MESSAGES);
const twoLetterUserLocale = locale.slice(0, 2);
if (availableLocales.includes(locale)) {
this.locale = locale;
} else if (availableLocales.includes(twoLetterUserLocale)) {
this.locale = twoLetterUserLocale;
} else {
this.locale = DEFAULT_LOCALE;
}
} else {
this.locale = DEFAULT_LOCALE;
}
this.message = {
text: Object.assign(
MESSAGES[DEFAULT_LOCALE].text,
MESSAGES[this.locale].text
),
func: Object.assign(
MESSAGES[DEFAULT_LOCALE].func,
MESSAGES[this.locale].func
),
};
}
}
export function setAxeLocalization(locale: string): axe.Locale {
// Get path to the locales directory of the dependent axe-core module
const require: NodeRequire = createRequire(import.meta.url);
const axeCoreLocalesPath: string = require
.resolve('axe-core')
.replace('axe.js', 'locales');
// Get the list of available axe-core locales
const localesList: string[] = fs
.readdirSync(axeCoreLocalesPath)
.reduce((availableLocales: string[], filename: string) => {
if (filename !== '_template.json' && filename.endsWith('.json')) {
availableLocales.push(filename.replace('.json', ''));
}
return availableLocales;
}, []);
// Set the axeLocale to the locale designated in the config file
let axeLocale: axe.Locale;
if (localesList.includes(locale)) {
axeLocale = JSON.parse(
fs
.readFileSync(path.join(axeCoreLocalesPath, `${locale}.json`))
.toString()
);
return axeLocale;
} else {
return {};
}
}