Skip to content

Utility to convert Django .po files to Excel .xlsx file and vice-versa for Django projects

Notifications You must be signed in to change notification settings

Sarvar-K/django-translations-xlsx-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Utility to convert Django .po files to Excel .xlsx file and vice-versa for Django projects

Library provides tools to:

  • Generate Excel .xlsx file based on Django .po files
  • Collect translatable strings from Django .po files to database table
  • Update Django .po files from Excel .xlsx file

Installation

 pip install git+https://github.com/Sarvar-K/django-translations-xlsx-converter

In requirements.txt file

git+https://github.com/Sarvar-K/django-translations-xlsx-converter

Installation in Docker: If installing via pip install, you will require git in image.

Setup

Add 'translations_xlsx_converter' to your INSTALLED_APPS in settings.py.

INSTALLED_APPS = [
    ...
    'translations_xlsx_converter',
]

You can also add the following global settings to settings.py (defaults are listed in the example below):

SERVICE_NAME_FOR_TRANSLATIONS_ORIGIN = "unknown"
TRANSLATIONS_EXCEL_FILE_NAME = "translations.xlsx"

Where:

  • SERVICE_NAME_FOR_TRANSLATIONS_ORIGIN - name of the Django project. This setting is used to populate origin_service column in database table when collecting translations to database.
  • TRANSLATIONS_EXCEL_FILE_NAME - name of an Excel file to export and import translations to and from Django .po files.

Add locale directory to your project on the manage.py directory tree level. Add empty subdirectories to locale directory with their names corresponding to the language codes of the translations. Both en and en_EN notations are supported.

The locale directory will hold both Django .po files and exported/imported Excel .xlsx file. The resulting project directory tree should be similar to this:

some_project/
    | some_project/
    |    | settings.py
    |    | urls.py
    | some_app/
    |    | __init__.py
    |    | admin.py
    |    | apps.py
    |    | models.py
    |    | tests.py
    |    | viwes.py
    | __init.py__
    | manage.py
    | locale/ <-----------
    |    | ru/
    |    | en/
    |    | uz/
    ...

Usage

Migrating

If using different postgresql schemas for different projects within the same database and the database table translations_xlsx_converter already exists, simply --fake migration with:

python3 manage.py migrate translations_xlsx_converter --fake

Exporting from Django to Excel

Run the following command to generate .xlsx file:

python3 manage.py translations_to_xlsx

Excel file will be generated from the .po files from respective locale subdirectories and placed in the locale directory.

Exporting from Django to database

Run migration if necessary and then run the following command to populate database table:

python3 manage.py translations_to_xlsx --use_db

The 'default' database from DATABASES settings key in settings.py will be used for this operation. The translations from .po files will be collected to translations_collector.translations_xlsx_converter (schema.table_name) database table.

If the translation key (#msgid) from .po file already exists in the database table and your wish to overwrite it, add --update argument to the command like so:

python3 manage.py translations_to_xlsx --use_db --update

Otherwise, this key will be skipped.

Currently, only en, ru and uz languages are supported for exporting to database.

Exporting from database to Excel

Run the following SQL query in your database:

SELECT 
	txc."key",
	txc.ru,
	txc.uz,
	txc.en,
	txc.origin_service
FROM translations_collector.translations_xlsx_converter AS txc
ORDER BY id;

Export the resulting dataset to Excel .xlsx file. Make sure to only include the rows of actual dataset in the export, e.g. skip the table information row with the (key, ru, uz, en, origin_service) cells.

Name the resulting .xlsx file in accordance with your TRANSLATIONS_EXCEL_FILE_NAME in settings.py if it is set. Otherwise, name your .xlsx file as translations.xlsx.

Put your .xlsx file in your Django project's locale directory.

Import translations from Excel to Django

Make sure your Excel .xlsx file is in the root of locale directory and run the following command:

python3 manage.py xlsx_to_translations

Translations of the Django .po files in the locale directory will be updated in accordance with the provided Excel .xlsx file.

You can also pass --xlsx_location="your_translations_location" file to specify path of the .xlsx file to import from relative to your manage.py module. For example, if your file is located in the parent directory relative to your manage.py module, you can run the command as follows:

python3 manage.py xlsx_to_translations --xlsx_location="../translations.xlsx"

The --xlsx_location argument takes precedence above TRANSLATIONS_EXCEL_FILE_NAME setting.

About

Utility to convert Django .po files to Excel .xlsx file and vice-versa for Django projects

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published