Feature: Importing & Exporting Translations
Relevant source files
The following files were used as context for generating this wiki page:- README.md
- api/src/controllers/import.controller.ts
- api/src/controllers/exports.controller.ts
- api/src/domain/http.ts
- webapp/src/app/projects/models/import.ts
- webapp/src/app/projects/components/import-locale/import-locale.component.html
- api/test/import.e2e-spec.ts
- api/test/export.e2e-spec.ts
- api/src/formatters/android-xml.ts
- api/src/formatters/php.spec.ts
The import and export functionality is a core feature of Traduora, enabling seamless integration with various development workflows. It allows teams to bring existing translation files into a project and extract translations from Traduora for use in their applications. This facilitates collaboration and automates the process of keeping translation files synchronized.
The system is designed to handle multiple file formats, manage new and existing translation keys (terms), and update translations for different locales within a project. All operations are accessible via a REST API, allowing for automation and integration with CI/CD pipelines.
Sources: README.md
Supported Formats
Traduora supports a wide range of common localization file formats for both import and export operations. The format is specified via a format query parameter in the API calls.
| Display Name | Code (format) | Extension |
|---|---|---|
| Android Resources (XML) | androidxml | .xml |
| CSV | csv | .csv |
| XLIFF 1.2 | xliff12 | .xliff |
| JSON Flat | jsonflat | .json |
| JSON | jsonnested | .json |
| YAML Flat | yamlflat | .yaml |
| YAML | yamlnested | .yaml |
| Java Properties | properties | .properties |
| Gettext (po) | po | .po |
| Strings | strings | .strings |
| PHP | php | .php |
| Resource RESX | resx | .resx |
Sources: webapp/src/app/projects/models/import.ts:7-20, api/src/domain/http.ts:409-422
API Endpoints
The import and export features are exposed through two main REST API endpoints.
Import Translations
This endpoint handles the uploading and processing of a translation file. It is a POST request that accepts multipart/form-data.
- Endpoint:
POST /api/v1/projects/:projectId/imports - Permissions: Requires project membership with
ImportTranslationaction permission.
Sources: api/src/controllers/import.controller.ts:50-58
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
locale | string | Yes | The locale code to import the translations into (e.g., en_US, de_DE). |
format | ImportExportFormat | Yes | The format of the uploaded file. See Supported Formats. |
Sources: api/src/domain/http.ts:424-432
Request Body
The request must be multipart/form-data and include a single file part named file. The file size is limited to 1MB.
Sources: api/src/controllers/import.controller.ts:53, api/src/controllers/import.controller.ts:56
Response Body
Upon successful import, the API returns a summary of the operation.
{
"data": {
"terms": {
"added": 1,
"skipped": 1
},
"translations": {
"upserted": 2
}
}
}
terms.added: Number of new terms created in the project from the file.terms.skipped: Number of terms in the file that already existed in the project.translations.upserted: Number of translations that were created or updated for the specified locale.
Sources: api/src/domain/http.ts:39-55, api/src/controllers/import.controller.ts:140-149
Export Translations
This endpoint allows downloading all translations for a specific locale in a chosen format.
- Endpoint:
GET /api/v1/projects/:projectId/exports - Permissions: Requires project membership with
ExportTranslationaction permission.
Sources: api/src/controllers/exports.controller.ts:36-44
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
locale | string | Yes | The locale code to export. |
format | ImportExportFormat | Yes | The desired output file format. See Supported Formats. |
fallbackLocale | string | No | A locale to use for translations that are missing in the primary locale. |
untranslated | boolean | No | If true, exports only terms that have empty translations. |
Sources: api/src/domain/http.ts:407-422
Response Body
The response is a file stream (application/octet-stream) containing the exported translations.
Sources: api/src/controllers/exports.controller.ts:41, api/src/controllers/exports.controller.ts:153-154
Architecture and Data Flow
Import Flow
The import process involves parsing the uploaded file, identifying new terms, and upserting translations within a single database transaction to ensure data integrity.
The following diagram illustrates the data flow for an import request.
Export Flow
The export process retrieves all terms for a project, joins their translations for a specified locale, and then formats the result into the desired file type.
The following diagram shows the export data flow.
Parsers and Exporters
A key part of the architecture is the use of an IntermediateTranslationFormat which acts as a standardized in-memory representation of translation data.
- Parsers: For each supported format, a parser function (e.g.,
androidXmlParser,jsonNestedParser) is responsible for converting the raw file content into theIntermediateTranslationFormat. - Exporters: Similarly, an exporter function (e.g.,
androidXmlExporter,jsonNestedExporter) takes theIntermediateTranslationFormatand serializes it into the target file format as a string or buffer.
This decoupled approach allows new formats to be added by simply implementing a new parser and exporter pair.
The ImportController and ExportsController use a switch statement to dynamically call the correct parser or exporter based on the format query parameter.
Sources: api/src/controllers/import.controller.ts:212-243, api/src/controllers/exports.controller.ts:157-188, api/src/formatters/android-xml-spec.ts
Frontend UI
The web application provides a user-friendly interface for the import feature.
The import component, located at webapp/src/app/projects/components/import-locale/import-locale.component.html, guides the user through the process:
- File Upload: A drag-and-drop area or a file-chooser button allows the user to select a translation file.
- Format and Locale Selection: Once a file is uploaded, dropdowns appear for the user to select the file format and the destination locale.
- Import Action: An "Import" button triggers the API call.
- Result Summary: After a successful import, the UI displays the summary returned by the API, showing how many terms were added or skipped and how many translations were upserted. The user is then given the option to navigate to the newly imported locale.
If the selected destination locale already exists, a warning is displayed informing the user that existing translations will be overwritten.
Sources: webapp/src/app/projects/components/import-locale/import-locale.component.html