Adding New Import/Export Formats
Relevant source files
The following files were used as context for generating this wiki page:- api/src/controllers/import.controller.ts
- api/src/controllers/exports.controller.ts
- api/src/domain/http.ts
- api/src/domain/formatters.ts
- webapp/src/app/projects/models/import.ts
- webapp/src/app/projects/components/import-locale/import-locale.component.ts
- api/src/formatters/php.spec.ts
- api/test/import.e2e-spec.ts
Traduora is designed to be extensible, allowing developers to add support for new translation file formats. The system uses a standardized intermediate data structure to decouple the core business logic from the specifics of any given file format. This allows for a modular approach where new formats can be integrated by creating a dedicated parser and exporter.
The core of this system revolves around converting any supported file format into an IntermediateTranslationFormat on import, and converting from this intermediate format into a specific file format on export. This guide outlines the architecture and the steps required to add a new format.
Core Concepts
Intermediate Translation Format
All import and export operations pivot around a common data structure, defined in api/src/domain/formatters.ts. This ensures that the application's core logic for handling terms and translations is independent of the file format.
The main interfaces are:
IntermediateTranslation: Represents a single key-value pair of a term and its translation.IntermediateTranslationFormat: A container for an array ofIntermediateTranslationobjects, and may include metadata like the ISO locale code.
// api/src/domain/formatters.ts:2-5
export interface IntermediateTranslation {
term: string;
translation: string;
}
// api/src/domain/formatters.ts:7-10
export interface IntermediateTranslationFormat {
translations: IntermediateTranslation[];
iso?: string;
}
Sources: api/src/domain/formatters.ts:2-10
The following diagram illustrates this central data structure.
Sources: api/src/domain/formatters.ts:2-10
Formatters: Parsers and Exporters
Each file format is supported by a pair of functions: a "parser" for imports and an "exporter" for exports.
- Parser: A function that takes the string content of an uploaded file and returns a
Promise<IntermediateTranslationFormat>. - Exporter: A function that takes an
IntermediateTranslationFormatobject and returns aPromise<string | Buffer>representing the final file content.
These functions are typically located in their own file within the api/src/formatters/ directory (e.g., jsonflat.ts, php.ts).
Sources: api/src/controllers/import.controller.ts:210-238, api/src/controllers/exports.controller.ts:153-181
ImportExportFormat Enum
A central enum, ImportExportFormat, is used throughout the API to identify supported formats. To add a new format, a new entry must be added to this enum.
// api/src/domain/http.ts:459-472
export enum ImportExportFormat {
AndroidXml = 'androidxml',
Csv = 'csv',
Xliff12 = 'xliff12',
JsonFlat = 'jsonflat',
JsonNested = 'jsonnested',
YamlFlat = 'yamlflat',
YamlNested = 'yamlnested',
Properties = 'properties',
Gettext = 'po',
Strings = 'strings',
Php = 'php',
ResX = 'resx',
}
Sources: api/src/domain/http.ts:459-472
Backend Implementation
The backend is responsible for processing the file transformations. This is primarily handled in the ImportController and ExportsController.
Import Flow
When a file is imported via the POST /api/v1/projects/:projectId/imports endpoint, the ImportController uses its parse method to delegate the file parsing to the appropriate formatter based on the format query parameter.
The diagram below shows the high-level flow for an import request.
To add a new format, you must add a new case to the switch statement within the parse method, calling your new parser function.
// api/src/controllers/import.controller.ts:210-238
private async parse(format: ImportExportFormat, contents): Promise<IntermediateTranslationFormat> {
try {
switch (format) {
case 'androidxml':
return await androidXmlParser(contents);
// ... other cases
case 'php':
return await phpParser(contents);
// ADD NEW CASE HERE
// case 'yournewformat':
// return await yourNewFormatParser(contents);
default:
throw new Error('Export format not implemented');
}
} catch (err) {
throw new BadRequestException('Malformed import file');
}
}
Sources: api/src/controllers/import.controller.ts:210-238
Export Flow
Similarly, when translations are exported via GET /api/v1/projects/:projectId/exports, the ExportsController fetches the data and uses its dump method to serialize it into the requested format.
Adding a new export format requires adding a corresponding case to the switch statement in the dump method.
// api/src/controllers/exports.controller.ts:153-181
private async dump(format: ImportExportFormat, data: IntermediateTranslationFormat): Promise<string | Buffer> {
switch (format) {
case 'androidxml':
return await androidXmlExporter(data);
// ... other cases
case 'php':
return await phpExporter(data);
// ADD NEW CASE HERE
// case 'yournewformat':
// return await yourNewFormatExporter(data);
default:
throw new Error('Export format not implemented');
}
}
Sources: api/src/controllers/exports.controller.ts:153-181
Frontend Implementation
For a new format to be selectable in the user interface, it must be added to the frontend configuration.
IMPORT_FORMATS Constant
The ImportLocaleComponent in the web application uses a constant array, IMPORT_FORMATS, to populate the "Select an import format" dropdown. A new format must be added to this array to appear in the UI.
| Property | Type | Description |
|---|---|---|
displayName | string | The user-friendly name displayed in the UI. |
code | string | The format identifier, must match ImportExportFormat enum value. |
extension | string | The common file extension, used for format guessing. |
Sources: webapp/src/app/projects/models/import.ts:1-5, webapp/src/app/projects/components/import-locale/import-locale.component.ts:84-97
// webapp/src/app/projects/models/import.ts:7-20
export const IMPORT_FORMATS: ImportFormat[] = [
{ displayName: 'Android Resources (XML)', extension: 'xml', code: 'androidxml' },
{ displayName: 'CSV', extension: 'csv', code: 'csv' },
{ displayName: 'XLIFF 1.2', extension: 'xliff', code: 'xliff12' },
{ displayName: 'JSON Flat', extension: 'json', code: 'jsonflat' },
// ... other formats
{ displayName: 'PHP', extension: 'php', code: 'php' },
{ displayName: 'Resource RESX', extension: 'resx', code: 'resx' },
// ADD NEW FORMAT OBJECT HERE
];
Sources: webapp/src/app/projects/models/import.ts:7-20
The export UI follows a similar pattern, populating its format dropdown from the same or a similar constant.
Step-by-Step Guide to Add a New Format
The following sequence diagram and steps summarize the end-to-end process for adding a new format.
-
Define Format Code (API): Add your new format's code (e.g.,
yourformat) to theImportExportFormatenum inapi/src/domain/http.ts.
Sources: api/src/domain/http.ts:459-472 -
Implement Formatter (API): Create a new file
api/src/formatters/yourformat.ts. Inside, implement and export two functions:yourformatParser(contents: string): Promise<IntermediateTranslationFormat>yourformatExporter(data: IntermediateTranslationFormat): Promise<string | Buffer>
Sources: api/src/controllers/import.controller.ts:210, api/src/controllers/exports.controller.ts:153
-
Add Tests (API): Create a corresponding test file
api/src/formatters/yourformat.spec.tsto ensure your parser and exporter work correctly with various inputs, including edge cases.
Sources: api/src/formatters/php.spec.ts -
Integrate Parser (API): In
api/src/controllers/import.controller.ts, import your newyourformatParserand add acasefor it in theparsemethod'sswitchstatement.
Sources: api/src/controllers/import.controller.ts:210-238 -
Integrate Exporter (API): In
api/src/controllers/exports.controller.ts, import your newyourformatExporterand add acasefor it in thedumpmethod'sswitchstatement.
Sources: api/src/controllers/exports.controller.ts:153-181 -
Expose in UI (Webapp): In
webapp/src/app/projects/models/import.ts, add a new object to theIMPORT_FORMATSarray with thedisplayName,code(matching the enum value), andextensionfor your new format.
Sources: webapp/src/app/projects/models/import.ts:7-20
After completing these steps, the new format will be fully integrated into the Traduora platform, available for both import and export operations. End-to-end tests should also be added in api/test/import.e2e-spec.ts and api/test/export.e2e-spec.ts to validate the full API flow.
Sources: api/test/import.e2e-spec.ts