Reference: synctranslations

This module contains the synctranslations command for the Translations app.

class translations.management.commands.synctranslations.Command[source]

The command which synchronizes the translations with the apps models configurations.

To use the synctranslations command:

$ python manage.py synctranslations
help

The command’s help text.

execute(*args, **options)[source]

Execute the Command with BaseCommand arguments.

This is an overriden version of the BaseCommand‘s execute() method. It defines the standard input on the Command.

Parameters
  • args (list) – The arguments of the BaseCommand‘s execute() method.

  • options (dict) – The keyword arguments of the BaseCommand‘s execute() method.

Returns

The return value of the BaseCommand‘s execute() method.

Return type

str or None

add_arguments(parser)[source]

Add the arguments that the Command accepts on an ArgumentParser.

Defines the different types of arguments that the Command accepts on the ArgumentParser.

Parameters

parser (ArgumentParser) – The parser to add the arguments that the Command accepts on.

get_content_types(*app_labels)[source]

Return the ContentTypes in some apps or all of them.

If the app labels are passed in it returns the ContentTypes in those apps, if nothing is passed in it returns the ContentTypes in all apps.

Parameters

app_labels (list(str)) – The apps in which to get the ContentTypes.

Returns

The ContentTypes in the apps.

Return type

QuerySet(ContentType)

To get the ContentTypes (in some apps):

from translations.management.commands.synctranslations import Command

command = Command()
content_types = command.get_content_types('sample').values_list(
   'app_label', 'model')

print(sorted(content_types))
[
    ('sample', 'city'),
    ('sample', 'continent'),
    ('sample', 'country'),
    ('sample', 'timezone'),
]

To get the ContentTypes (in all apps):

from translations.management.commands.synctranslations import Command

command = Command()
content_types = command.get_content_types().values_list(
   'app_label', 'model')

print(sorted(content_types))
[
    ('admin', 'logentry'),
    ('auth', 'group'),
    ('auth', 'permission'),
    ('auth', 'user'),
    ('contenttypes', 'contenttype'),
    ('sample', 'city'),
    ('sample', 'continent'),
    ('sample', 'country'),
    ('sample', 'timezone'),
    ('sessions', 'session'),
    ('translations', 'translation'),
]
get_obsolete_translations(content_types)[source]

Return the obsolete translations of some ContentTypes.

Returns the obsolete translations of the ContentTypes based on the current configurations of their models.

Parameters

content_types (QuerySet(ContentType)) – The ContentTypes to get the obsolete translations of.

Returns

The obsolete translations of the ContentTypes.

Return type

QuerySet(Translation)

To get the obsolete translations of some ContentTypes:

from translations.management.commands.synctranslations import Command

command = Command()
content_types = command.get_content_types('sample')
obsolete_translations = command.get_obsolete_translations(content_types)

print(obsolete_translations)
<QuerySet []>
log_obsolete_translations(obsolete_translations)[source]

Log the details of some obsolete translations.

Logs the model and field details of the obsolete translations.

Parameters

obsolete_translations (QuerySet(Translation)) – The obsolete translations to log the details of.

ask_yes_no(message, default=None)[source]

Ask the user for yes or no with a message and a default value.

Prompts the user with the message asking them for a yes or no answer, optionally a default value can be set for the answer.

Parameters
  • message (str) – The question to ask the user for yes or no with.

  • default (str or bool or None) – The default value for the answer.

Returns

The user’s yes or no answer.

Return type

bool

should_run_synchronization()[source]

Return whether to run the synchronization or not.

Determines whether the synchronization should run or not. It does so by making sure that the user is aware of the risks. If the user is using a TTY it asks them whether they are sure or not and if the user is NOT using a TTY they have to explicitly declare that they are sure in the command.

Returns

whether to run the synchronization or not.

Return type

bool

handle(*app_labels, **options)[source]

Run the Command with the configured arguments.

This is an overriden version of the BaseCommand‘s handle() method. It synchronizes the translations with the apps models configurations.

Parameters
  • app_labels (list(str)) – The apps to synchronize the translations with the models configurations of.

  • options (dict(str, str)) – The configured options of the Command.