Skip to content
Open
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/core/baseProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ abstract class BaseProcessor {
abstract loadIntoTree(filePathOrBuffer: ProcessorInput): Promise<AACTree>;

// Process texts (e.g., apply translations) and return new file/buffer
// If targetLocale is provided, it attempts to add the language to the file (multilingual support)
// Otherwise, it replaces the existing text (translation/localization)
abstract processTexts(
filePathOrBuffer: ProcessorInput,
translations: Map<string, string>,
outputPath: string
outputPath: string,
targetLocale?: string
): Promise<BinaryOutput>;

// Save tree structure back to file/buffer
Expand Down
3 changes: 2 additions & 1 deletion src/processors/applePanelsProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ class ApplePanelsProcessor extends BaseProcessor {
async processTexts(
filePathOrBuffer: ProcessorInput,
translations: Map<string, string>,
outputPath: string
outputPath: string,
_targetLocale?: string
): Promise<Uint8Array> {
// Load the tree, apply translations, and save to new file
const tree = await this.loadIntoTree(filePathOrBuffer);
Expand Down
90 changes: 73 additions & 17 deletions src/processors/astericsGridProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,8 @@ class AstericsGridProcessor extends BaseProcessor {
async processTexts(
filePathOrBuffer: ProcessorInput,
translations: Map<string, string>,
outputPath: string
outputPath: string,
targetLocale?: string
): Promise<Uint8Array> {
await Promise.resolve();
let content = readTextFromInput(filePathOrBuffer);
Expand All @@ -1323,7 +1324,7 @@ class AstericsGridProcessor extends BaseProcessor {
const grdFile: AstericsGridFile = JSON.parse(content);

// Apply translations directly to the JSON structure for comprehensive coverage
this.applyTranslationsToGridFile(grdFile, translations);
this.applyTranslationsToGridFile(grdFile, translations, targetLocale);

// Write the translated file
writeTextToPath(outputPath, JSON.stringify(grdFile, null, 2));
Expand All @@ -1332,39 +1333,86 @@ class AstericsGridProcessor extends BaseProcessor {

private applyTranslationsToGridFile(
grdFile: AstericsGridFile,
translations: Map<string, string>
translations: Map<string, string>,
targetLocale?: string
): void {
grdFile.grids.forEach((grid: GridData) => {
// Translate grid labels
if (grid.label) {
Object.keys(grid.label).forEach((lang) => {
const originalText = grid.label[lang];
if (originalText && translations.has(originalText)) {
if (typeof grid.label === 'string') {
const originalText = grid.label as string;
if (translations.has(originalText)) {
const translation = translations.get(originalText);
if (translation !== undefined) {
grid.label[lang] = translation;
if (targetLocale) {
// Upgrade to object format
grid.label = {
en: originalText, // Assume 'en' for legacy string labels
[targetLocale]: translation,
};
} else {
grid.label = translation as any;
}
}
}
});
} else {
Object.keys(grid.label).forEach((lang) => {
const originalText = grid.label[lang];
if (originalText && translations.has(originalText)) {
const translation = translations.get(originalText);
if (translation !== undefined) {
if (targetLocale) {
grid.label[targetLocale] = translation;
} else {
grid.label[lang] = translation;
}
}
}
});
}
}

// Translate grid elements
grid.gridElements.forEach((element: GridElement) => {
// Translate element labels
if (element.label) {
Object.keys(element.label).forEach((lang) => {
const originalText = element.label[lang];
if (originalText && translations.has(originalText)) {
if (typeof element.label === 'string') {
const originalText = element.label as string;
if (translations.has(originalText)) {
const translation = translations.get(originalText);
if (translation !== undefined) {
element.label[lang] = translation;
if (targetLocale) {
// Upgrade to object format
element.label = {
en: originalText, // Assume 'en' for legacy string labels
[targetLocale]: translation,
};
} else {
element.label = translation as any;
}
}
}
});
} else {
Object.keys(element.label).forEach((lang) => {
const originalText = element.label[lang];
if (originalText && translations.has(originalText)) {
const translation = translations.get(originalText);
if (translation !== undefined) {
if (targetLocale) {
element.label[targetLocale] = translation;
} else {
element.label[lang] = translation;
}
}
}
});
}
}

// Translate word forms
if (element.wordForms) {
// Word forms are typically specific to a language, so adding a target locale might require structure change
// For now, we only translate in place if no targetLocale, or skip if targetLocale is set (as word forms are language specific)
if (element.wordForms && !targetLocale) {
element.wordForms.forEach((wordForm: WordForm) => {
if (wordForm.value && translations.has(wordForm.value)) {
const translation = translations.get(wordForm.value);
Expand All @@ -1377,13 +1425,17 @@ class AstericsGridProcessor extends BaseProcessor {

// Translate action-specific texts
element.actions.forEach((action: GridAction) => {
this.applyTranslationsToAction(action, translations);
this.applyTranslationsToAction(action, translations, targetLocale);
});
});
});
}

private applyTranslationsToAction(action: GridAction, translations: Map<string, string>): void {
private applyTranslationsToAction(
action: GridAction,
translations: Map<string, string>,
targetLocale?: string
): void {
switch (action.modelName) {
case 'GridActionSpeakCustom':
if (action.speakText && typeof action.speakText === 'object') {
Expand All @@ -1393,7 +1445,11 @@ class AstericsGridProcessor extends BaseProcessor {
if (typeof originalText === 'string' && translations.has(originalText)) {
const translation = translations.get(originalText);
if (translation !== undefined) {
speakTextMap[lang] = translation;
if (targetLocale) {
speakTextMap[targetLocale] = translation;
} else {
speakTextMap[lang] = translation;
}
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/processors/dotProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ class DotProcessor extends BaseProcessor {
async processTexts(
filePathOrBuffer: ProcessorInput,
translations: Map<string, string>,
outputPath: string
outputPath: string,
_targetLocale?: string
): Promise<Uint8Array> {
await Promise.resolve();
const content = readTextFromInput(filePathOrBuffer);
Expand Down
3 changes: 2 additions & 1 deletion src/processors/excelProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class ExcelProcessor extends BaseProcessor {
async processTexts(
_filePathOrBuffer: ProcessorInput,
_translations: Map<string, string>,
outputPath: string
outputPath: string,
_targetLocale?: string
): Promise<Uint8Array> {
await Promise.resolve();
console.warn('ExcelProcessor.processTexts is not implemented yet.');
Expand Down
3 changes: 2 additions & 1 deletion src/processors/gridsetProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,8 @@ class GridsetProcessor extends BaseProcessor {
async processTexts(
filePathOrBuffer: ProcessorInput,
translations: Map<string, string>,
outputPath: string
outputPath: string,
_targetLocale?: string
): Promise<Uint8Array> {
// Load the tree, apply translations, and save to new file
const tree = await this.loadIntoTree(filePathOrBuffer);
Expand Down
Loading
Loading