updatelang: Respect the target ordering in individual phrases

When matching the target id in a phrase, if there is more than one match
we always use the final one.  This allows us to easily specify a
default/wildcard entry that gets overridden by a target-specific one.

The list of targets was sorted alphabetically to ensure consistent ordering
from one run of the tooling to the next.

However, if a phrase contained both device-specific phrases as well as a
generic "feature" fallback, alphabetical sorting may screw things up, as
the "feature default" was no longer at the top of the list. This is
known to be an issue for LANG_TIME_SET_BUTTON and LANG_TIME_REVERT, but
may affect other phrases as well.

(To be blunt, we shouldn't be mixing feature and device-specific targets
 in this context.  The "feature default" should be removed in favor of
 target-specific entries, but in this specific case it looks to be a
 real PITA due to incomplete keymaps)

Consequently, work around this by sorting the target list within each phrase
based on the ordering in the master (ie English) language file.

Change-Id: Id32439c179a98663f414530fb36012f9b217c1b6
This commit is contained in:
Solomon Peachy 2025-10-02 11:54:07 -04:00
parent a7cf86e5c1
commit 79e4e075e6

View file

@ -13,6 +13,8 @@ use utf8;
use File::Basename;
use Unicode::Normalize;
use strict;
use open qw( :std :encoding(UTF-8) );
binmode(STDOUT, ":encoding(UTF-8)");
@ -32,10 +34,12 @@ sub parselangfile {
#'source' => {},
#'dest' => {},
#'voice' => {},
#'targetorder' => {},
'notes' => "",
'new' => 0
);
my %thisphrase = %empty;
my %targetorder;
open(FH, "<$filename") || die ("Can't open $filename");
my @lines = <FH>;
@ -85,6 +89,9 @@ sub parselangfile {
$w = NFD($w); # Unicode decompose
$thisphrase{$pos}->{$l} = $w;
# Store the ordering of the targets.
$targetorder{$l} = scalar(keys(%targetorder)) if (!defined($targetorder{$l}));
}
}
if ($line eq '</voice>' ||
@ -94,8 +101,11 @@ sub parselangfile {
$pos = 'phrase';
} elsif ($line eq '</phrase>') {
my %copy = %thisphrase;
my %targetordercopy = %targetorder;
$copy{'targetorder'} = \%targetordercopy;
$phrases{$id} = \%copy;
%thisphrase = %empty;
%targetorder = ();
$pos = 'lang';
$id = '';
} elsif ($line eq '<source>') {
@ -166,7 +176,6 @@ sub reduceformat($) {
return $out;
}
##################
if($#ARGV != 2) {
@ -526,7 +535,50 @@ if ($ENV{'ENGLISHORDER'}) {
@finalorder = @langorder;
}
foreach my $id (@finalorder) {
my ($id, %tgtorder);
# When LANG_TIME_SET_BUTTON and LANG_TIME_REVERT are fixed to not
# include a default 'rtc' value, then this abobmination can be
# replaced with { $a cmp $b } to restore simple alphabetical
# ordering. We should NOT be mixing feature and target options
# in the same phrase.
sub bytarget {
my $xa = $a;
my $xb = $b;
my $rval = 0;
# print "ORDER: ". join("|", %tgtorder) . "\n";
my $da = defined($tgtorder{$xa});
if (!$da) { # try the first entry of a list
my @foo = split(',', $xa);
$xa = $foo[0];
$da = defined($tgtorder{$xa});
}
my $db = defined($tgtorder{$xb});
if (!$db) { # try the first entry of a list
my @foo = split(',', $xb);
$xb = $foo[0];
$db = defined($tgtorder{$xb});
}
if ($xa eq "*") {
$rval = -1;
} elsif ($xb eq "*") {
$rval = 1;
} elsif ($da && $db) {
$rval = ($tgtorder{$xa} <=> $tgtorder{$xb});
} elsif (!$da && !$db) {
$rval = ($xa cmp $xb);
} elsif ($da && !$db) {
$rval = -1;
} elsif (!$da && $db) {
$rval = 1;
}
# print "~~~ '$xa' vs '$xb' ($da/$db) = $rval\n";
return $rval;
}
foreach $id (@finalorder) {
if (!defined($english{$id})) {
next;
}
@ -535,6 +587,8 @@ foreach my $id (@finalorder) {
# phrase
%lp = %{$lang{$id}{'phrase'}};
%tgtorder = %{ $english{$id}{'targetorder'}};
# Drop all deprecated phrases?
# next if ($lp{'desc'} eq 'deprecated');
@ -553,7 +607,7 @@ foreach my $id (@finalorder) {
# source
%lp = combinetgts(%{$lang{$id}{'source'}});
print $fh " <source>\n";
foreach my $tgt (sort(keys(%lp))) {
foreach my $tgt (sort bytarget keys(%lp)) {
my $w = NFC($lp{$tgt});
if ($w eq 'none') {
print $fh " $tgt: $w\n";
@ -566,7 +620,7 @@ foreach my $id (@finalorder) {
# dest
%lp = combinetgts(%{$lang{$id}{'dest'}});
print $fh " <dest>\n";
foreach my $tgt (sort(keys(%lp))) {
foreach my $tgt (sort bytarget keys(%lp)) {
my $w = NFC($lp{$tgt});
if ($w eq 'none') {
print $fh " $tgt: $w\n";
@ -579,7 +633,7 @@ foreach my $id (@finalorder) {
# voice
%lp = combinetgts(%{$lang{$id}{'voice'}});
print $fh " <voice>\n";
foreach my $tgt (sort(keys(%lp))) {
foreach my $tgt (sort bytarget keys(%lp)) {
my $w = NFC($lp{$tgt});
if ($w eq 'none') {
print $fh " $tgt: $w\n";