-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[i18n] extract untracked translations and prettier logging (#35171)
* extract untracked translations and prettier logging * self code review * Update src/dev/run_i18n_check.ts Co-Authored-By: Bamieh <ahmadbamieh@gmail.com> * updating listR * run new i18n_check and fix errors * kbnEmbeddables -> embeddableApi * remove any type * Update src/legacy/core_plugins/embeddable_api/public/panel/panel_header/panel_actions/add_panel/add_panel_flyout.tsx Co-Authored-By: Stacey Gammon <gammon@elastic.co> * Update src/legacy/core_plugins/embeddable_api/public/panel/panel_header/panel_actions/add_panel/add_panel_action.tsx Co-Authored-By: Stacey Gammon <gammon@elastic.co> * self code review fixes * fix extract and integrate scripts * ts-ignore js file
- Loading branch information
Showing
26 changed files
with
381 additions
and
146 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { ToolingLog } from '@kbn/dev-utils'; | ||
import { integrateLocaleFiles, I18nConfig } from '..'; | ||
|
||
export interface I18nFlags { | ||
fix: boolean; | ||
ignoreIncompatible: boolean; | ||
ignoreUnused: boolean; | ||
ignoreMissing: boolean; | ||
} | ||
|
||
export function checkCompatibility(config: I18nConfig, flags: I18nFlags, log: ToolingLog) { | ||
const { fix, ignoreIncompatible, ignoreUnused, ignoreMissing } = flags; | ||
return config.translations.map(translationsPath => ({ | ||
task: async ({ messages }: { messages: Map<string, { message: string }> }) => { | ||
// If `fix` is set we should try apply all possible fixes and override translations file. | ||
await integrateLocaleFiles(messages, { | ||
dryRun: !fix, | ||
ignoreIncompatible: fix || ignoreIncompatible, | ||
ignoreUnused: fix || ignoreUnused, | ||
ignoreMissing: fix || ignoreMissing, | ||
sourceFileName: translationsPath, | ||
targetFileName: fix ? translationsPath : undefined, | ||
config, | ||
log, | ||
}); | ||
}, | ||
title: `Compatibility check with ${translationsPath}`, | ||
})); | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { | ||
I18nConfig, | ||
matchEntriesWithExctractors, | ||
normalizePath, | ||
readFileAsync, | ||
ErrorReporter, | ||
} from '..'; | ||
import { createFailError } from '../../run'; | ||
|
||
function filterEntries(entries: string[], exclude: string[]) { | ||
return entries.filter((entry: string) => | ||
exclude.every((excludedPath: string) => !normalizePath(entry).startsWith(excludedPath)) | ||
); | ||
} | ||
|
||
export async function extractUntrackedMessagesTask({ | ||
path, | ||
config, | ||
reporter, | ||
}: { | ||
path?: string | string[]; | ||
config: I18nConfig; | ||
reporter: any; | ||
}) { | ||
const inputPaths = Array.isArray(path) ? path : [path || './']; | ||
const availablePaths = Object.values(config.paths); | ||
const ignore = availablePaths.concat([ | ||
'**/build/**', | ||
'**/webpackShims/**', | ||
'**/__fixtures__/**', | ||
'**/packages/kbn-i18n/**', | ||
'**/packages/kbn-plugin-generator/sao_template/**', | ||
'**/packages/kbn-ui-framework/generator-kui/**', | ||
'**/target/**', | ||
'**/test/**', | ||
'**/scripts/**', | ||
'**/src/dev/**', | ||
'**/target/**', | ||
'**/dist/**', | ||
]); | ||
for (const inputPath of inputPaths) { | ||
const categorizedEntries = await matchEntriesWithExctractors(inputPath, { | ||
additionalIgnore: ignore, | ||
mark: true, | ||
absolute: true, | ||
}); | ||
|
||
for (const [entries, extractFunction] of categorizedEntries) { | ||
const files = await Promise.all( | ||
filterEntries(entries, config.exclude) | ||
.filter(entry => { | ||
const normalizedEntry = normalizePath(entry); | ||
return !availablePaths.some( | ||
availablePath => | ||
normalizedEntry.startsWith(`${normalizePath(availablePath)}/`) || | ||
normalizePath(availablePath) === normalizedEntry | ||
); | ||
}) | ||
.map(async (entry: any) => ({ | ||
name: entry, | ||
content: await readFileAsync(entry), | ||
})) | ||
); | ||
|
||
for (const { name, content } of files) { | ||
const reporterWithContext = reporter.withContext({ name }); | ||
for (const [id] of extractFunction(content, reporterWithContext)) { | ||
const errorMessage = `Untracked file contains i18n label (${id}).`; | ||
reporterWithContext.report(createFailError(errorMessage)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function extractUntrackedMessages(srcPaths: string[], config: I18nConfig) { | ||
return srcPaths.map(srcPath => ({ | ||
title: `Checking untracked messages in ${srcPath}`, | ||
task: async (context: { reporter: ErrorReporter }) => { | ||
const { reporter } = context; | ||
const initialErrorsNumber = reporter.errors.length; | ||
const result = await extractUntrackedMessagesTask({ path: srcPath, config, reporter }); | ||
if (reporter.errors.length === initialErrorsNumber) { | ||
return result; | ||
} | ||
|
||
throw reporter; | ||
}, | ||
})); | ||
} |
This file contains 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
Oops, something went wrong.