@@ -904,25 +904,77 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
904904 FILE * outfile = err ? stderr : stdout ;
905905 int need_newline ;
906906
907+ const char * usage_prefix = _ ("usage: %s" );
908+ /*
909+ * The translation could be anything, but we can count on
910+ * msgfmt(1)'s --check option to have asserted that "%s" is in
911+ * the translation. So compute the length of the "usage: "
912+ * part. We are assuming that the translator wasn't overly
913+ * clever and used e.g. "%1$s" instead of "%s", there's only
914+ * one "%s" in "usage_prefix" above, so there's no reason to
915+ * do so even with a RTL language.
916+ */
917+ size_t usage_len = strlen (usage_prefix ) - strlen ("%s" );
918+ /*
919+ * TRANSLATORS: the colon here should align with the
920+ * one in "usage: %s" translation.
921+ */
922+ const char * or_prefix = _ (" or: %s" );
923+ /*
924+ * TRANSLATORS: You should only need to translate this format
925+ * string if your language is a RTL language (e.g. Arabic,
926+ * Hebrew etc.), not if it's a LTR language (e.g. German,
927+ * Russian, Chinese etc.).
928+ *
929+ * When a translated usage string has an embedded "\n" it's
930+ * because options have wrapped to the next line. The line
931+ * after the "\n" will then be padded to align with the
932+ * command name, such as N_("git cmd [opt]\n<8
933+ * spaces>[opt2]"), where the 8 spaces are the same length as
934+ * "git cmd ".
935+ *
936+ * This format string prints out that already-translated
937+ * line. The "%*s" is whitespace padding to account for the
938+ * padding at the start of the line that we add in this
939+ * function. The "%s" is a line in the (hopefully already
940+ * translated) N_() usage string, which contained embedded
941+ * newlines before we split it up.
942+ */
943+ const char * usage_continued = _ ("%*s%s" );
944+ const char * prefix = usage_prefix ;
945+ int saw_empty_line = 0 ;
946+
907947 if (!usagestr )
908948 return PARSE_OPT_HELP ;
909949
910950 if (!err && ctx && ctx -> flags & PARSE_OPT_SHELL_EVAL )
911951 fprintf (outfile , "cat <<\\EOF\n" );
912952
913- fprintf_ln (outfile , _ ("usage: %s" ), _ (* usagestr ++ ));
914- while (* usagestr && * * usagestr )
915- /*
916- * TRANSLATORS: the colon here should align with the
917- * one in "usage: %s" translation.
918- */
919- fprintf_ln (outfile , _ (" or: %s" ), _ (* usagestr ++ ));
920953 while (* usagestr ) {
921- if (* * usagestr )
922- fprintf_ln (outfile , _ (" %s" ), _ (* usagestr ));
923- else
924- fputc ('\n' , outfile );
925- usagestr ++ ;
954+ const char * str = _ (* usagestr ++ );
955+ struct string_list list = STRING_LIST_INIT_DUP ;
956+ unsigned int j ;
957+
958+ if (!saw_empty_line && !* str )
959+ saw_empty_line = 1 ;
960+
961+ string_list_split (& list , str , '\n' , -1 );
962+ for (j = 0 ; j < list .nr ; j ++ ) {
963+ const char * line = list .items [j ].string ;
964+
965+ if (saw_empty_line && * line )
966+ fprintf_ln (outfile , _ (" %s" ), line );
967+ else if (saw_empty_line )
968+ fputc ('\n' , outfile );
969+ else if (!j )
970+ fprintf_ln (outfile , prefix , line );
971+ else
972+ fprintf_ln (outfile , usage_continued ,
973+ (int )usage_len , "" , line );
974+ }
975+ string_list_clear (& list , 0 );
976+
977+ prefix = or_prefix ;
926978 }
927979
928980 need_newline = 1 ;
0 commit comments