Skip to content

Commit

Permalink
Merge pull request #7 from lyqht/v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lyqht authored Aug 5, 2023
2 parents e4678a8 + 7f082cf commit 585bb11
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 23 deletions.
3 changes: 0 additions & 3 deletions playground/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const authKey = process.env.deepl_api_key as string;
const translator = new deepl.Translator(authKey);
const playgroundPath = "playground"
const inputFilePath = path.join(playgroundPath, "nested.json");
// note that if outputFileNamePattern is set, outputFileNamePrefix is ignored
const outputFileNamePattern = path.join(playgroundPath, "locales/{language}/nested.json");
const outputFileNamePrefix = path.join(playgroundPath, "translated_simple_");
const startTagForNoTranslate = "<!-- keep -->";
const endTagForNoTranslate = "<!-- /keep -->";

Expand All @@ -21,7 +19,6 @@ const targetLanguages = ["ja"] as deepl.TargetLanguageCode[];
translator,
inputFilePath,
outputFileNamePattern,
outputFileNamePrefix,
startTagForNoTranslate,
endTagForNoTranslate,
tempFilePath,
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ const inputFilePath = path.join(
process.env.GITHUB_WORKSPACE as string,
process.env.input_file_path as string,
);
const outputFileNamePrefix = path.join(
process.env.GITHUB_WORKSPACE as string,
process.env.output_file_name_prefix as string,
) || 'translated_';
const outputFileNamePattern = path.join(
process.env.GITHUB_WORKSPACE as string,
process.env.output_file_name_pattern as string,
Expand All @@ -37,7 +33,6 @@ const fileExtensionsThatAllowForIgnoringBlocks = [".html", ".xml", ".md", ".txt"
await main({
translator,
inputFilePath,
outputFileNamePrefix,
outputFileNamePattern,
startTagForNoTranslate,
endTagForNoTranslate,
Expand Down
8 changes: 3 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ interface HTMLlikeParams {
export interface MainFunctionParams extends HTMLlikeParams {
translator: Translator;
inputFilePath: string;
outputFileNamePrefix: string;
outputFileNamePattern?: string;
outputFileNamePattern: string;
tempFilePath: string;
fileExtensionsThatAllowForIgnoringBlocks: string[];
targetLanguages: TargetLanguageCode[];
Expand All @@ -22,7 +21,6 @@ export async function main(params: MainFunctionParams) {
const {
translator,
inputFilePath,
outputFileNamePrefix,
outputFileNamePattern,
startTagForNoTranslate,
endTagForNoTranslate,
Expand Down Expand Up @@ -92,7 +90,7 @@ export async function main(params: MainFunctionParams) {
}
const resultText = removeKeepTagsFromString(translatedText);

const outputFileName = buildOutputFileName(targetLang, fileExtension, outputFileNamePrefix, outputFileNamePattern);
const outputFileName = buildOutputFileName(targetLang, outputFileNamePattern);
const outputFolderPath = path.dirname(outputFileName);
if (!fs.existsSync(outputFolderPath)) {
fs.mkdirSync(outputFolderPath, { recursive: true });
Expand All @@ -117,7 +115,7 @@ export async function main(params: MainFunctionParams) {

for (const targetLanguage of targetLanguages) {
const targetLang = targetLanguage as TargetLanguageCode;
const outputFileName = buildOutputFileName(targetLang, fileExtension, outputFileNamePrefix, outputFileNamePattern);
const outputFileName = buildOutputFileName(targetLang, outputFileNamePattern);
const resultJson = JSON.stringify(translatedResults[targetLang]);
const outputFolderPath = path.dirname(outputFileName);
if (!fs.existsSync(outputFolderPath)) {
Expand Down
8 changes: 2 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,9 @@ const translateRecursive = async (

function buildOutputFileName(
targetLang: string,
fileExtension?: string,
outputFileNamePrefix?: string,
outputFileNamePattern?: string,
outputFileNamePattern: string,
) {
return outputFileNamePattern
? `${outputFileNamePattern.replace("{language}", targetLang)}`
: `${outputFileNamePrefix}${targetLang}${fileExtension}`;
return `${outputFileNamePattern.replace("{language}", targetLang)}`
}

export {
Expand Down
8 changes: 4 additions & 4 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("main - HTMLlike files", () => {

const fakeInputFileFolderPath = "test";
const fakeInputFilename = "inputFilePath.md";
const fakeOutputFileNamePrefix = `${fakeInputFileFolderPath}/`;
const fakeOutputFileNamePattern = `${fakeInputFileFolderPath}/`;
const fakeTempFilePath = "to_translate.txt";
const fakeReadFileResult = Buffer.from("Your mocked data here");

Expand Down Expand Up @@ -59,7 +59,7 @@ describe("main - HTMLlike files", () => {
const testParams: MainFunctionParams = {
translator: mockTranslator,
inputFilePath: `${fakeInputFileFolderPath}/${fakeInputFilename}`,
outputFileNamePrefix: fakeOutputFileNamePrefix,
outputFileNamePrefix: fakeOutputFileNamePattern,
tempFilePath: fakeTempFilePath,
fileExtensionsThatAllowForIgnoringBlocks: [".html", ".xml", ".md"],
targetLanguages: ["de"],
Expand Down Expand Up @@ -90,7 +90,7 @@ describe("main - JSON files", () => {

const fakeInputFileFolderPath = "test";
const fakeInputFilename = "inputFilePath.json";
const fakeOutputFileNamePrefix = `${fakeInputFileFolderPath}/`;
const fakeOutputFileNamePattern = `${fakeInputFileFolderPath}/{language}.json`;
const fakeTempFilePath = "to_translate.txt";
const testJSON = {
welcome: "Welcome, {name}!",
Expand Down Expand Up @@ -126,7 +126,7 @@ describe("main - JSON files", () => {
const testParams: MainFunctionParams = {
translator: mockTranslator,
inputFilePath: `${fakeInputFileFolderPath}/${fakeInputFilename}`,
outputFileNamePrefix: fakeOutputFileNamePrefix,
outputFileNamePattern: fakeOutputFileNamePattern,
tempFilePath: fakeTempFilePath,
fileExtensionsThatAllowForIgnoringBlocks: [".html", ".xml", ".md"],
targetLanguages: ["de"],
Expand Down

0 comments on commit 585bb11

Please sign in to comment.