Make the notes instruction text and marker translatable
Marco Ricci

Marco Ricci commited on 2025-01-23 10:51:36
Zeige 2 geänderte Dateien mit 40 Einfügungen und 16 Löschungen.


The instruction text for editing notes is natural language text, and
should therefore be translated, but was overlooked when determining
translatable messages.

To ensure that the instruction text and the marker cannot be mismatched,
the marker is translated separately and inserted verbatim after the
instruction text.
... ...
@@ -807,6 +807,25 @@ class Label(enum.Enum):
807 807
         'STRONGLY discouraged from using a stored passphrase.',
808 808
     )
809 809
     """"""
810
+    DERIVEPASSPHRASE_VAULT_NOTES_INSTRUCTION_TEXT = commented(
811
+        "This instruction text is shown above the user's old stored notes "
812
+        'for this service, if any.  '
813
+        'The next line is the cut marking defined in '
814
+        'Label.DERIVEPASSPHRASE_VAULT_NOTES_MARKER.'
815
+    )(
816
+        'Label :: Help text :: Explanation',
817
+        """\
818
+# Enter notes below the line with the cut mark (ASCII scissors and
819
+# dashes).  Lines above the cut mark (such as this one) will be ignored.
820
+#
821
+# If you wish to clear the notes, leave everything beyond the cut mark
822
+# blank.  However, if you leave the *entire* file blank, also removing
823
+# the cut mark, then the edit is aborted, and the old notes contents are
824
+# retained.
825
+#
826
+""",
827
+    )
828
+    """"""
810 829
     DEPRECATED_COMMAND_LABEL = commented(
811 830
         'We use this format string to indicate, at the beginning '
812 831
         "of a command's help text, that this command is deprecated.",
... ...
@@ -816,6 +835,16 @@ class Label(enum.Enum):
816 835
         flags='python-brace-format',
817 836
     )
818 837
     """"""
838
+    DERIVEPASSPHRASE_VAULT_NOTES_MARKER = commented(
839
+        'The marker for separating the text from '
840
+        'Label.DERIVEPASSPHRASE_VAULT_NOTES_INSTRUCTION_TEXT '
841
+        "from the user's input (below the marker).  "
842
+        'The first line starting with this label marks the separation point.',
843
+    )(
844
+        'Label :: Help text :: Marker',
845
+        '# - - - - - >8 - - - - - >8 - - - - - >8 - - - - - >8 - - - - -',
846
+    )
847
+    """"""
819 848
     DEBUG_OPTION_HELP_TEXT = commented(
820 849
         '',
821 850
     )(
... ...
@@ -2160,20 +2160,6 @@ def _validate_length(
2160 2160
     return int_value
2161 2161
 
2162 2162
 
2163
-DEFAULT_NOTES_TEMPLATE = """\
2164
-# Enter notes below the line with the cut mark (ASCII scissors and
2165
-# dashes).  Lines above the cut mark (such as this one) will be ignored.
2166
-#
2167
-# If you wish to clear the notes, leave everything beyond the cut mark
2168
-# blank.  However, if you leave the *entire* file blank, also removing
2169
-# the cut mark, then the edit is aborted, and the old notes contents are
2170
-# retained.
2171
-#
2172
-# - - - - - >8 - - - - - >8 - - - - - >8 - - - - - >8 - - - - -
2173
-"""
2174
-DEFAULT_NOTES_MARKER = '# - - - - - >8 - - - - -'
2175
-
2176
-
2177 2163
 @derivepassphrase.command(
2178 2164
     'vault',
2179 2165
     context_settings={'help_option_names': ['-h', '--help']},
... ...
@@ -2826,15 +2812,24 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
2826 2812
     if edit_notes:
2827 2813
         assert service is not None
2828 2814
         configuration = get_config()
2829
-        text = DEFAULT_NOTES_TEMPLATE + configuration['services'].get(
2815
+        notes_instructions = _msg.TranslatedString(
2816
+            _msg.Label.DERIVEPASSPHRASE_VAULT_NOTES_INSTRUCTION_TEXT
2817
+        )
2818
+        notes_marker = _msg.TranslatedString(
2819
+            _msg.Label.DERIVEPASSPHRASE_VAULT_NOTES_MARKER
2820
+        )
2821
+        old_notes_value = configuration['services'].get(
2830 2822
             service, cast('_types.VaultConfigServicesSettings', {})
2831 2823
         ).get('notes', '')
2824
+        text = '\n'.join([
2825
+            str(notes_instructions), str(notes_marker), old_notes_value
2826
+        ])
2832 2827
         notes_value = click.edit(text=text)
2833 2828
         if notes_value is not None:
2834 2829
             notes_lines = collections.deque(notes_value.splitlines(True))  # noqa: FBT003
2835 2830
             while notes_lines:
2836 2831
                 line = notes_lines.popleft()
2837
-                if line.startswith(DEFAULT_NOTES_MARKER):
2832
+                if line.startswith(str(notes_marker)):
2838 2833
                     notes_value = ''.join(notes_lines)
2839 2834
                     break
2840 2835
             else:
2841 2836