I had noticed that the Swedish hyphenation mode was sticking to
our English pages after groff_mmse(7) in groff-man-pages.pdf.

This is proving to be quite the devil to deal with, thanks to
the large number of constraints imposed by batch rendering of
multiple pages (which may be in different languages), the
wrapping of the macro package itself by andoc.tmac (because man
pages can be written with different *roff macro packages),
the user-customizability of the hyphenation mode, and that
damned bit in the hyphenation mode that has ****-all to do with
the hyphenation patterns but instead is a knob for suppressing
hyphenation when a page location trap is about to spring.

I'm including a diff of me trying to solve the problem within
all of these constraints, and it works for our use case, but I
am not happy at all with the level of complexity it demands.

I propose instead to revise the semantics of the man(7) `HY`
macro and make it a Boolean.  Hyphenation on or off--if on, you
get what our macro files decide for you.  Does any user really
want to decide the details of the hyphenation mode themselves?

I have a pretty strong suspicion that all people use `HY` for in
the first place is mashing a big "SHUT HYPHENATION OFF" button
anyway (and that's what man-db _man_(1)'s --nh option already
does for the 99% who use `man` instead of `groff -man`).

(Or _mandoc_(1), but it doesn't affect the decision here because
as I understand it, it doesn't support automatic hyphenation and
Ingo doesn't plan to ever add it.)

If someone really wants to seize control of the hyphenation
mode, they're free to redefine the `hy` request in man.local.

I invite them to keep all the pieces.

Next steps:

I need to write a regression test to catch the case I observed
(the hyphenation in question was a ghastly "g-lyph").

Then implement `HY` boolification, and see how much less complex
that route really is.

The heart of this is probably, in `TH`, turning

+verbatim+
.  hy \\n[HY]
-verbatim-

into

+verbatim+
.  if \\n[HY] .hy \\n[an*hyphenation-mode]
-verbatim-

or maybe

+verbatim+
.  ie \\n[HY] .hy \\n[an*hyphenation-mode]
.  el         .hy 0
-verbatim-

To support recovery from rogue pages that sneakily turned it on.

Note for anyone reading hastily: the commit below is *not*
pushed as of this writing.

commit 8874719524b1384ba370e50b8c1ac9a18699ac76
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
Date:   Sat Feb 5 03:07:30 2022 +1100

    XXX describe better

+verbatim+
diff --git a/ChangeLog b/ChangeLog
index 4dee43d21..3970c6b2d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2022-02-04  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+	[man]: Refactor localization again.
+
+	We need a locale-dependent hyphenation mode default, but (1)
+	the mode selected is dependent on continuous rendering (to
+	suppress hyphenation breaks before page location traps, which is
+	not a locale-dependent feature at all) and (2) the mode needs to
+	be overridable at the command line or from the man.local file
+	via the `HY` register, and that override preserved through the
+	formatting of all documents processed in that run.
+
+	* tmac/an.tmac (an*initialize-hyphenation-default): New macro
+	selects (but does not yet set) a hyphenation mode if _not_
+	overridden by the `HY` register.
+	(TH): Call `an*initialize-hyphenation-default` before setting
+	hyphenation mode.
+	(initialization): Set a flag, `an*did-user-specify-HY` to
+	indicate whether a hyphenation mode override is in effect.
+	'andoc.tmac' might have run before us, so honor its value.
+	Because 'man.local' might set `HY`, perform
+	These tests are also performed after 'man.local' is processed.
+
 2022-02-03  G. Branden Robinson <g.branden.robinson@gmail.com>
 
 	[docs]: Present several fundamental concepts.
diff --git a/tmac/an.tmac b/tmac/an.tmac
index 7b18a7d68..44fea656c 100644
--- a/tmac/an.tmac
+++ b/tmac/an.tmac
@@ -177,6 +177,17 @@
 .  ds an*section9 Kernel Developer's Manual\"
 ..
 .
+.de an*initialize-hyphenation-default
+.  \" These _should_ be initialized by the localization file, but if
+.  \" that's been disabled, set up reasonable English defaults.
+.  if !r an*hyphenation-mode-base .nr an*hyphenation-mode-base 4
+.  if !r an*hyphenation-mode-trap .nr an*hyphenation-mode-trap 6
+.  \" No page breaks occur in continuous rendering.
+.  ie \\n[cR] .nr an*hyphenation-mode \\n[an*hyphenation-mode-base]
+.  el         .nr an*hyphenation-mode \\n[an*hyphenation-mode-trap]
+.  if !\\n[an*do-override-HY] .nr HY \\n[an*hyphenation-mode]
+..
+.
 .\" Write a bookmark/anchor/link target $2 at hierarchical depth $1.
 .de an*bookmark
 .  if '\\*[.T]'pdf' \{\
@@ -232,6 +243,9 @@
 .    \}
 .  \}
 .
+.  \" The hyphenation mode also varies by locale, but is overridable.
+.  an*initialize-hyphenation-default
+.
 .  ps \\n[PS]u
 .  vs \\n[VS]u
 .  ad \\*[AD]
@@ -1048,9 +1062,20 @@
 .\" Load man macro extensions.
 .mso an-ext.tmac
 .
-.\" Load local modifications.
+.\" Load local modifications.  Take note if `HY` is overridden; if the
+.\" register wasn't set before we source 'man.local' and is afterwards,
+.\" it was.  We can't just test `HY` itself because it might already
+.\" have been set when rendering a previous page, and 'andoc.tmac' is
+.\" now reloading us.
+.if !r HY .nr an*did-man-local-set-HY 0
+.
 .mso man.local
 .
+.if r HY \
+.  if r an*did-man-local-set-HY .nr an*did-man-local-set-HY 1
+.
+.if !r an*did-man-local-set-HY .nr an*did-man-local-set-HY 0
+.
 .\" Set each rendering parameter only if its -[dr] option or man.local
 .\" did not.
 .
@@ -1145,12 +1170,22 @@
 .\}
 .
 .\" hyphenation mode
-.if !r HY \{\
-.  \" No page breaks occur in continuous rendering.
-.  ie \n[cR] .nr HY 4
-.  el        .nr HY 6
+.\"
+.\" The user can override the hyphenation mode.  If not, its
+.\" determination is complicated by continuous rendering and
+.\" localization issues.
+.if r andoc*did-user-specify-HY \
+.  nr an*did-user-specify-HY \n[andoc*did-user-specify-HY]
+.
+.if !r an*did-user-specify-HY \{\
+.  nr an*did-user-specify-HY 0
+.  if r HY .nr an*did-user-specify-HY 1
 .\}
 .
+.nr an*do-override-HY 0
+.if (\n[an*did-man-local-set-HY] : \n[an*did-user-specify-HY]) \
+.  nr an*do-override-HY 1
+.
 .\" standard indentation
 .if !r IN \{\
 .  \" We select an integer indentation value in nroff mode because this
diff --git a/tmac/andoc.tmac b/tmac/andoc.tmac
index ee4d38789..557ca23ab 100644
--- a/tmac/andoc.tmac
+++ b/tmac/andoc.tmac
@@ -103,6 +103,12 @@
 .de EN
 ..
 .
+.\" Detect and remember user override of hyphenation mode.
+.if !r andoc*did-user-specify-HY \{\
+.  nr andoc*did-user-specify-HY 0
+.  if r HY .nr andoc*did-user-specify-HY 1
+.\}
+.
 .cp \n[*groff_andoc_tmac_C]
 .do rr *groff_andoc_tmac_C
 .
diff --git a/tmac/cs.tmac b/tmac/cs.tmac
index d70508660..7bbbb5efb 100644
--- a/tmac/cs.tmac
+++ b/tmac/cs.tmac
@@ -25,6 +25,13 @@
 .cp 0
 .
 .
+.\" If switching locales, save the trap flag part of the hyphenation
+.\" value so we know which mode to use for the upcoming locale.
+.ds locale*hyflag base\"
+.if d locale \
+.  if (\n[.hy] = \n[\*[locale]-hyphenation-mode-trap]) \
+.    ds locale*hyflag trap\"
+.
 .ds locale czech\"
 .
 .
@@ -108,19 +115,17 @@
 .ds \*[locale]-tuesday Úterý\"
 .ds \*[locale]-wednesday Støeda\"
 .
+.\" Czech hyphenation (\lefthyphenmin=2, \righthyphenmin=2)
+.nr \*[locale]-hyphenation-mode-base 1
+.nr \*[locale]-hyphenation-mode-trap 2
 .
-.\" Activate the translations
+.hy \n[\*[locale]-hyphenation-mode-\*[locale*hyflag]]
+.rm locale*hyflag
 .
+.\" Activate the translations and hyphenation mode defaults.
 .mso trans.tmac
 .
 .
-.\" man package
-.if d an \{\
-.	ie \n[cR] .nr HY 1
-.	el        .nr HY 2
-.\}
-.
-.
 .\" ms package
 .if r GS \{\
 .	\" update the date
@@ -163,16 +168,6 @@
 .
 .ss 12 0
 .
-.\" Czech hyphenation (\lefthyphenmin=2, \righthyphenmin=2)
-.\"
-.\" Hyphenation value 2 (bit 1) may already have been set by troffrc or
-.\" a macro package; preserve it.
-.ie (\n[.hy]%2=0 & (\n[.hy]%4)) \
-.	hy 2
-.el \
-.	hy 1
-.\}
-.
 .hcode á á  Á á
 .hcode è è  È è
 .hcode ï ï  Ï ï
diff --git a/tmac/de.tmac b/tmac/de.tmac
index 3690349cc..28c36f671 100644
--- a/tmac/de.tmac
+++ b/tmac/de.tmac
@@ -25,6 +25,13 @@
 .cp 0
 .
 .
+.\" If switching locales, save the trap flag part of the hyphenation
+.\" value so we know which mode to use for the upcoming locale.
+.ds locale*hyflag base\"
+.if d locale \
+.  if (\n[.hy] = \n[\*[locale]-hyphenation-mode-trap]) \
+.    ds locale*hyflag trap\"
+.
 .ds locale german\"
 .
 .
@@ -109,16 +116,15 @@
 .ds \*[locale]-wednesday Mittwoch\"
 .
 .
-.\" Activate the translations
-.
-.mso trans.tmac
+.\" German hyphenation (\lefthyphenmin=2, \righthyphenmin=2)
+.nr \*[locale]-hyphenation-mode-base 1
+.nr \*[locale]-hyphenation-mode-trap 2
 .
+.hy \n[\*[locale]-hyphenation-mode-\*[locale*hyflag]]
+.rm locale*hyflag
 .
-.\" man package
-.if d an \{\
-.	ie \n[cR] .nr HY 1
-.	el        .nr HY 2
-.\}
+.\" Activate the translations and hyphenation mode defaults.
+.mso trans.tmac
 .
 .
 .\" ms package
@@ -163,15 +169,6 @@
 .
 .ss 12 0
 .
-.\" German hyphenation (\lefthyphenmin=2, \righthyphenmin=2)
-.\"
-.\" Hyphenation value 2 (bit 1) may already have been set by troffrc or
-.\" a macro package; preserve it.
-.ie (\n[.hy]%2=0 & (\n[.hy]%4)) \
-.	hy 2
-.el \
-.	hy 1
-.
 .hcode ä ä  â â  à à  á á  ã ã  å å  æ æ
 .hcode ç ç
 .hcode é é  è è  ë ë  ê ê
diff --git a/tmac/en.tmac b/tmac/en.tmac
index 45dafa405..192e37af1 100644
--- a/tmac/en.tmac
+++ b/tmac/en.tmac
@@ -24,18 +24,23 @@
 .cp 0
 .
 .
-.ds locale english\"
+.\" If switching locales, save the trap flag part of the hyphenation
+.\" value so we know which mode to use for the upcoming locale.
+.ds locale*hyflag base\"
+.if d locale \
+.  if (\n[.hy] = \n[\*[locale]-hyphenation-mode-trap]) \
+.    ds locale*hyflag trap\"
 .
-.ss 12
+.ds locale english\"
 .
 .\" English hyphenation (\lefthyphenmin=2, \righthyphenmin=3)
-.\"
-.\" Hyphenation value 2 (bit 1) may already have been set by troffrc or
-.\" a macro package; preserve it.
-.ie (\n[.hy]%2=0 & (\n[.hy]%4)) \
-.	hy 6
-.el \
-.	hy 4
+.nr \*[locale]-hyphenation-mode-base 4
+.nr \*[locale]-hyphenation-mode-trap 6
+.
+.hy \n[\*[locale]-hyphenation-mode-\*[locale*hyflag]]
+.rm locale*hyflag
+.
+.ss 12
 .
 .hla en
 .hpf hyphen.en
diff --git a/tmac/fr.tmac b/tmac/fr.tmac
index 0d81c4363..6434012ff 100644
--- a/tmac/fr.tmac
+++ b/tmac/fr.tmac
@@ -25,6 +25,13 @@
 .cp 0
 .
 .
+.\" If switching locales, save the trap flag part of the hyphenation
+.\" value so we know which mode to use for the upcoming locale.
+.ds locale*hyflag base\"
+.if d locale \
+.  if (\n[.hy] = \n[\*[locale]-hyphenation-mode-trap]) \
+.    ds locale*hyflag trap\"
+.
 .ds locale french\"
 .
 .
@@ -108,9 +115,14 @@
 .ds \*[locale]-tuesday Mardi\"
 .ds \*[locale]-wednesday Mercredi\"
 .
+.\" French hyphenation (\lefthyphenmin=2, \righthyphenmin=3)
+.nr \*[locale]-hyphenation-mode-base 4
+.nr \*[locale]-hyphenation-mode-trap 6
 .
-.\" Activate the translations
+.hy \n[\*[locale]-hyphenation-mode-\*[locale*hyflag]]
+.rm locale*hyflag
 .
+.\" Activate the translations and hyphenation mode defaults.
 .mso trans.tmac
 .
 .
@@ -156,15 +168,6 @@
 .
 .ss 12 0
 .
-.\" French hyphenation (\lefthyphenmin=2, \righthyphenmin=3)
-.\"
-.\" Hyphenation value 2 (bit 1) may already have been set by troffrc or
-.\" a macro package; preserve it.
-.ie (\n[.hy]%2=0 & (\n[.hy]%4)) \
-.	hy 6
-.el \
-.	hy 4
-.
 .hcode à à  À à
 .hcode â â  Â â
 .hcode ç ç  Ç ç
diff --git a/tmac/it.tmac b/tmac/it.tmac
index 29e385f81..92c409f15 100644
--- a/tmac/it.tmac
+++ b/tmac/it.tmac
@@ -25,6 +25,13 @@
 .cp 0
 .
 .
+.\" If switching locales, save the trap flag part of the hyphenation
+.\" value so we know which mode to use for the upcoming locale.
+.ds locale*hyflag base\"
+.if d locale \
+.  if (\n[.hy] = \n[\*[locale]-hyphenation-mode-trap]) \
+.    ds locale*hyflag trap\"
+.
 .ds locale italian\"
 .
 .
@@ -108,19 +115,17 @@
 .ds \*[locale]-tuesday Marted\[i `]\"
 .ds \*[locale]-wednesday Mercoled\[i `]\"
 .
+.\" Italian hyphenation (\lefthyphenmin=2, \righthyphenmin=2)
+.nr \*[locale]-hyphenation-mode-base 1
+.nr \*[locale]-hyphenation-mode-trap 2
 .
-.\" Activate the translations
+.hy \n[\*[locale]-hyphenation-mode-\*[locale*hyflag]]
+.rm locale*hyflag
 .
+.\" Activate the translations and hyphenation mode defaults.
 .mso trans.tmac
 .
 .
-.\" man package
-.if d an \{\
-.	ie \n[cR] .nr HY 1
-.	el        .nr HY 2
-.\}
-.
-.
 .\" ms package
 .if r GS \{\
 .	\" update the date
@@ -160,16 +165,6 @@
 .
 .ss 12 0
 .
-.
-.\" Italian hyphenation (\lefthyphenmin=2, \righthyphenmin=2)
-.\"
-.\" Hyphenation value 2 (bit 1) may already have been set by troffrc or
-.\" a macro package; preserve it.
-.ie (\n[.hy]%2=0 & (\n[.hy]%4)) \
-.	hy 2
-.el \
-.	hy 1
-.
 .hla it
 .hpf hyphen.it
 .
diff --git a/tmac/sv.tmac b/tmac/sv.tmac
index 3e2102272..9212b837c 100644
--- a/tmac/sv.tmac
+++ b/tmac/sv.tmac
@@ -25,6 +25,13 @@
 .cp 0
 .
 .
+.\" If switching locales, save the trap flag part of the hyphenation
+.\" value so we know which mode to use for the upcoming locale.
+.ds locale*hyflag base\"
+.if d locale \
+.  if (\n[.hy] = \n[\*[locale]-hyphenation-mode-trap]) \
+.    ds locale*hyflag trap\"
+.
 .ds locale swedish\"
 .
 .
@@ -108,19 +115,17 @@
 .ds \*[locale]-tuesday tisdag\"
 .ds \*[locale]-wednesday onsdag\"
 .
+.\" Swedish hyphenation (\lefthyphenmin=1, \righthyphenmin=2)
+.nr \*[locale]-hyphenation-mode-base 32
+.nr \*[locale]-hyphenation-mode-trap 34
 .
-.\" Activate the translations
+.hy \n[\*[locale]-hyphenation-mode-\*[locale*hyflag]]
+.rm locale*hyflag
 .
+.\" Activate the translations and hyphenation mode defaults.
 .mso trans.tmac
 .
 .
-.\" man package
-.if d an \{\
-.	ie \n[cR] .nr HY 32
-.	el        .nr HY 34
-.\}
-.
-.
 .\" ms package
 .if r GS \{\
 .	\" update the date
@@ -145,15 +150,6 @@
 .
 .ss 12 0
 .
-.\" Swedish hyphenation (\lefthyphenmin=1, \righthyphenmin=2)
-.\"
-.\" Hyphenation value 2 (bit 1) may already have been set by troffrc or
-.\" a macro package; preserve it.
-.ie (\n[.hy]%2=0 & (\n[.hy]%4)) \
-.	hy 34
-.el \
-.	hy 32
-.
 .hcode å å  Å å
 .hcode ä ä  Ä ä
 .hcode ö ö  Ö ö
diff --git a/tmac/tests/localization-works.sh b/tmac/tests/localization-works.sh
index de05823e4..e51d47f32 100755
--- a/tmac/tests/localization-works.sh
+++ b/tmac/tests/localization-works.sh
@@ -27,7 +27,8 @@ wail () {
     fail=YES
 }
 
-input='.tm .hy=\\n[.hy]'
+input='.TH localization-test 1 2022-02-04 "groff test suite"
+.tm .hy=\\n[.hy]'
 
 output=$(echo "$input" | "$groff" -Tascii -P-cbou -mcs 2>&1)
 echo 'checking raw troff with -mcs' >&2
diff --git a/tmac/trans.tmac b/tmac/trans.tmac
index a06426b3e..e79b0a9e5 100644
--- a/tmac/trans.tmac
+++ b/tmac/trans.tmac
@@ -48,6 +48,8 @@
 .	als an*section7 \*[locale]-man-section7
 .	als an*section8 \*[locale]-man-section8
 .	als an*section9 \*[locale]-man-section9
+.	nr an*hyphenation-mode-base \n[\*[locale]-hyphenation-mode-base]
+.	nr an*hyphenation-mode-trap \n[\*[locale]-hyphenation-mode-trap]
 .\}
 .
 .rr trans*is-man
-verbatim-
