django-admin makemessages --no-obsolete doesn't seem to be working

时间:2022-02-06 00:06:06

First of all, I am expecting --no-obsolete would comment out msgid and msgstr if gettext is deleted, right?

首先,我期待 - 如果删除了gettext,no-obsolete将注释掉msgid和msgstr,对吧?

How I am testing is:

我的测试方法是:

  1. I wrote gettext("some string here") in view
  2. 我在视图中写了gettext(“some string here”)

  3. I ran makemessages command
  4. 我跑了makemessages命令

  5. It wrote a .po file as expected
  6. 它按预期编写了一个.po文件

  7. Then I deleted gettext() line from view and saved file, verified runserver working.
  8. 然后我从视图中删除了gettext()行并保存了文件,验证了runserver的工作情况。

  9. I ran makemessages --no-obsolete and it has not made any changes to .po file.
  10. 我运行makemessages --no-obsolete并没有对.po文件进行任何更改。

.po file content extract .

.po文件内容提取。

#. Translators: This message is a test of wrap line
#: servers/views.py:31
msgid "Do let me know if it works."
msgstr ""

dev environment

Django = 1.11
OS = Mac/Ubuntu 14.04

Django = 1.11 OS = Mac / Ubuntu 14.04

settings.py

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE = (
      os.path.join(os.path.dirname(__file__), "locale"),
)

3 个解决方案

#1


1  

What the --no-obsolete does is to run a command called msgattrib with the --no-obsolete option on the content the po file. A typical case would be you generate your po file with makemessages, you get this:

--no-obsolete的作用是在po文件的内容上运行名为msgattrib的命令,并使用--no-obsolete选项。一个典型的例子是你用makemessages生成你的po文件,你得到这个:

#: servers/views.py:31
msgid "Do let me know if it works."
msgstr ""

Then you translate:

然后你翻译:

#: servers/views.py:31
msgid "Do let me know if it works."
msgstr "translation"

Then you remove the gettext entry, it'll still by default keep the translation, but mark it as obsolete.

然后你删除了gettext条目,它仍然默认保留翻译,但将其标记为过时。

#: servers/views.py:31
#~msgid "Do let me know if it works."
#~msgstr "translation"

If you set --no-obsolete option, then once your po file is done, it'll run msgattr with no-obsolete option. This will remove lines tagged with #~. See https://linux.die.net/man/1/msgattrib

如果你设置--no-obsolete选项,那么一旦你的po文件完成,它将运行带有no-obsolete选项的msgattr。这将删除标有#〜的行。请参阅https://linux.die.net/man/1/msgattrib

But, the way makemessages is built, is that this will be called once the po file is written. But if there are no gettext in the files being processed, then it won't write to the po file. It'll just stop before getting to this msgattrib command. The po file you see is the one generated by the previous makemessages command. So the no-obsolete won't do anything.

但是,makemessages的构建方式是,一旦写入po文件就会调用它。但是如果正在处理的文件中没有gettext,那么它将不会写入po文件。它将在获取此msgattrib命令之前停止。您看到的po文件是由之前的makemessages命令生成的文件。所以不过时也不会做任何事情。

There's no real solution to this. the no-obsolete option doesn't deal with the cases where you don't have any gettext to process.

对此没有真正的解决方案。没有过时的选项不处理你没有任何gettext要处理的情况。

#2


1  

So I think @JulienGrégoire was right about the fact that if there is no translation processed then the --no-obsolete won't work. There needs to be at least one translation captured for --no-obsolete to work.

所以我认为@JulienGrégoire是正确的,如果没有处理翻译,那么--no-obsolete将不起作用。需要至少捕获一个转换为--no-obsolete才能工作。

But the solution to this quite simple. You can update your settings.py to define LANGUAGES like below

但解决这个问题非常简单。您可以更新settings.py以定义如下的语言

from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
)

Now your settings will always generate a translation. So it will make sure that you get --no-obsolete working every time you use it

现在,您的设置将始终生成翻译。因此,它将确保您每次使用它时都不会过时

#3


1  

Now with the help of Julien and Tarun, I found following observations.

现在在Julien和Tarun的帮助下,我发现了以下观察结果。

python manage.py makemessages -l <locale>

python manage.py makemessages -l <​​locale>

If there is no gettext in the file being processed, the above command won't write/update .po file. That means if the corresponding .po file earlier had entries for msgstr and msgid, then it won't remove those entries unless file being processed had at least one gettext.

如果正在处理的文件中没有gettext,则上述命令将不会写入/更新.po文件。这意味着如果相应的.po文件之前有msgstr和msgid的条目,那么它将不会删除这些条目,除非正在处理的文件至少有一个gettext。

Note: Above behavior is irrespective of --no-obsolete

注意:以上行为与--no-obsolete无关

Now to make the --no-obsolete work as expected we need to follow the steps below.

现在要按预期进行 - 不过时的工作,我们需要按照以下步骤操作。

  1. First thing run python manage.py makemessages -l <locale>, this would write .po file with msgid and msgstr.

    首先运行python manage.py makemessages -l <​​locale>,这将使用msgid和msgstr编写.po文件。

  2. Now set msgstr and run python manage.py compilemessages -l <locale>. This command writes .mo file in the same directory as .po file.

    现在设置msgstr并运行python manage.py compilemessages -l <​​locale>。此命令将.mo文件写入与.po文件相同的目录中。

  3. Now next time when you run makemessages again (without --no-obsolete), .po and .mo files are compared and missing/deleted gettext are commented in .po file.

    现在下次再次运行makemessages(没有--no-obsolete)时,会比较.po和.mo文件,并在.po文件中注释缺失/删除​​的gettext。

  4. And when you run makemessages --no-obsolete, commented entries are removed from the .po file.
  5. 当你运行makemessages --no-obsolete时,注释的条目将从.po文件中删除。

E.g

if you have 3 gettext entries, and you run makemessages first time, it would write 3 msgid and 3 msgstr in .po file. Now if you remove all gettext entries, .po file won't be updated after you run makemessages again, but if your keep at least 1 gettext entry in same file and run makemessages again, it would delete all msgid and msgstr for deleted gettext entries.

如果你有3个gettext条目,并且你第一次运行makemessages,它会在.po文件中写入3 msgid和3 msgstr。现在,如果删除所有gettext条目,再次运行makemessages后将不会更新.po文件,但如果您在同一文件中保留至少1个gettext条目并再次运行makemessages,它将删除已删除的gettext条目的所有msgid和msgstr 。

But if you run compilemessages after makemessages, .mo file is created and then for subsequent makemessages commands .po and .mo files are compared and then msgid and msgstr is commented in .po file for deleted gettext entries.

但是如果你在makemessages之后运行compilemessages,则创建.mo文件,然后为后续makemessages命令比较.po和.mo文件,然后在.po文件中注释msgid和msgstr以获取已删除的gettext条目。

Then finally when you run makemessages with --no-obsolete option the commented messages from .po files are deleted permanently.

最后,当您使用--no-obsolete选项运行makemessages时,将永久删除来自.po文件的注释消息。

#1


1  

What the --no-obsolete does is to run a command called msgattrib with the --no-obsolete option on the content the po file. A typical case would be you generate your po file with makemessages, you get this:

--no-obsolete的作用是在po文件的内容上运行名为msgattrib的命令,并使用--no-obsolete选项。一个典型的例子是你用makemessages生成你的po文件,你得到这个:

#: servers/views.py:31
msgid "Do let me know if it works."
msgstr ""

Then you translate:

然后你翻译:

#: servers/views.py:31
msgid "Do let me know if it works."
msgstr "translation"

Then you remove the gettext entry, it'll still by default keep the translation, but mark it as obsolete.

然后你删除了gettext条目,它仍然默认保留翻译,但将其标记为过时。

#: servers/views.py:31
#~msgid "Do let me know if it works."
#~msgstr "translation"

If you set --no-obsolete option, then once your po file is done, it'll run msgattr with no-obsolete option. This will remove lines tagged with #~. See https://linux.die.net/man/1/msgattrib

如果你设置--no-obsolete选项,那么一旦你的po文件完成,它将运行带有no-obsolete选项的msgattr。这将删除标有#〜的行。请参阅https://linux.die.net/man/1/msgattrib

But, the way makemessages is built, is that this will be called once the po file is written. But if there are no gettext in the files being processed, then it won't write to the po file. It'll just stop before getting to this msgattrib command. The po file you see is the one generated by the previous makemessages command. So the no-obsolete won't do anything.

但是,makemessages的构建方式是,一旦写入po文件就会调用它。但是如果正在处理的文件中没有gettext,那么它将不会写入po文件。它将在获取此msgattrib命令之前停止。您看到的po文件是由之前的makemessages命令生成的文件。所以不过时也不会做任何事情。

There's no real solution to this. the no-obsolete option doesn't deal with the cases where you don't have any gettext to process.

对此没有真正的解决方案。没有过时的选项不处理你没有任何gettext要处理的情况。

#2


1  

So I think @JulienGrégoire was right about the fact that if there is no translation processed then the --no-obsolete won't work. There needs to be at least one translation captured for --no-obsolete to work.

所以我认为@JulienGrégoire是正确的,如果没有处理翻译,那么--no-obsolete将不起作用。需要至少捕获一个转换为--no-obsolete才能工作。

But the solution to this quite simple. You can update your settings.py to define LANGUAGES like below

但解决这个问题非常简单。您可以更新settings.py以定义如下的语言

from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French')),
)

Now your settings will always generate a translation. So it will make sure that you get --no-obsolete working every time you use it

现在,您的设置将始终生成翻译。因此,它将确保您每次使用它时都不会过时

#3


1  

Now with the help of Julien and Tarun, I found following observations.

现在在Julien和Tarun的帮助下,我发现了以下观察结果。

python manage.py makemessages -l <locale>

python manage.py makemessages -l <​​locale>

If there is no gettext in the file being processed, the above command won't write/update .po file. That means if the corresponding .po file earlier had entries for msgstr and msgid, then it won't remove those entries unless file being processed had at least one gettext.

如果正在处理的文件中没有gettext,则上述命令将不会写入/更新.po文件。这意味着如果相应的.po文件之前有msgstr和msgid的条目,那么它将不会删除这些条目,除非正在处理的文件至少有一个gettext。

Note: Above behavior is irrespective of --no-obsolete

注意:以上行为与--no-obsolete无关

Now to make the --no-obsolete work as expected we need to follow the steps below.

现在要按预期进行 - 不过时的工作,我们需要按照以下步骤操作。

  1. First thing run python manage.py makemessages -l <locale>, this would write .po file with msgid and msgstr.

    首先运行python manage.py makemessages -l <​​locale>,这将使用msgid和msgstr编写.po文件。

  2. Now set msgstr and run python manage.py compilemessages -l <locale>. This command writes .mo file in the same directory as .po file.

    现在设置msgstr并运行python manage.py compilemessages -l <​​locale>。此命令将.mo文件写入与.po文件相同的目录中。

  3. Now next time when you run makemessages again (without --no-obsolete), .po and .mo files are compared and missing/deleted gettext are commented in .po file.

    现在下次再次运行makemessages(没有--no-obsolete)时,会比较.po和.mo文件,并在.po文件中注释缺失/删除​​的gettext。

  4. And when you run makemessages --no-obsolete, commented entries are removed from the .po file.
  5. 当你运行makemessages --no-obsolete时,注释的条目将从.po文件中删除。

E.g

if you have 3 gettext entries, and you run makemessages first time, it would write 3 msgid and 3 msgstr in .po file. Now if you remove all gettext entries, .po file won't be updated after you run makemessages again, but if your keep at least 1 gettext entry in same file and run makemessages again, it would delete all msgid and msgstr for deleted gettext entries.

如果你有3个gettext条目,并且你第一次运行makemessages,它会在.po文件中写入3 msgid和3 msgstr。现在,如果删除所有gettext条目,再次运行makemessages后将不会更新.po文件,但如果您在同一文件中保留至少1个gettext条目并再次运行makemessages,它将删除已删除的gettext条目的所有msgid和msgstr 。

But if you run compilemessages after makemessages, .mo file is created and then for subsequent makemessages commands .po and .mo files are compared and then msgid and msgstr is commented in .po file for deleted gettext entries.

但是如果你在makemessages之后运行compilemessages,则创建.mo文件,然后为后续makemessages命令比较.po和.mo文件,然后在.po文件中注释msgid和msgstr以获取已删除的gettext条目。

Then finally when you run makemessages with --no-obsolete option the commented messages from .po files are deleted permanently.

最后,当您使用--no-obsolete选项运行makemessages时,将永久删除来自.po文件的注释消息。