"""
Use pretranslate to add translations that allready exist to a pofile
"""

import polib
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils.translation import gettext, override


class Command(BaseCommand):
    help = __doc__

    def add_arguments(self, parser):
        parser.add_argument("path", help="path to the pofile you want to check")
        parser.add_argument("-l", "--language", default=settings.LANGUAGE_CODE)
        parser.add_argument("--save", action="store_true")

    def handle(self, *args, **options):
        po = polib.pofile(options["path"])

        for entry in po:
            if not entry.msgstr:
                with override(options["language"]):
                    translation = gettext(entry.msgid)
                    if translation != entry.msgid:
                        entry.msgstr = translation

        if options["save"]:
            po.save(options["path"])
        else:
            self.stdout.write(str(po))
