TOKYO—The Japanese government will soon reach formal agreement to settle a massive class-action lawsuit involving people who contracted hepatitis B through childhood vaccination, expected to cost up to 3.2 trillion yen ($39 billion) in damages to compensate hundreds of thousands of victims.

The long-fought lawsuit, originally started in 1989, is a legacy of the time when needles and syringes were routinely reused to inoculate a number of children to save costs, a practice that persisted in Japan until 1988. Victims' groups...