Those determined to have committed such acts faced up to seven years in prison, with terms of up to five years for “any person who attempts to” engage in homosexual acts.

The laws’ wording closely resembles that of many other former British colonies where homosexuality is illegal. England banned sex between men in the 16th century, a law that was struck down in 1981 by the European Court of Human Rights. Yet by then, activists say, early British attitudes toward homosexuality had been imposed on the country’s former colonial subjects. Of the more than 70 countries globally that criminalize homosexuality, more than half were once under British dominion.

Challenges to these laws have been met with mixed success. In 2018, India’s Supreme Court struck down its “unnatural offenses” provision, declaring the law “irrational, indefensible and manifestly arbitrary.” Other Commonwealth nations that have revoked anti-sodomy laws include Australia, Belize and South Africa.

But last month, Kenyan judges upheld laws criminalizing gay sex, a move that activists feared would set back the push to expand L.G.B.T. rights in Africa.

Homophobia is widely entrenched on the continent, with gay sex outlawed in over 30 countries. In several northern African nations, including Somalia and Sudan, homosexuality is punishable by death; offenders in Sierra Leone, Tanzania and Uganda face life in prison.

Even as laws change, confronting beliefs could prove challenging. A 2013 survey by the Pew Research Center reported “widespread rejection” of homosexuality in six African nations — including South Africa, which is widely considered to have some of the most progressive gay rights legislation in the world.

On Tuesday, Judge Leburu referred to Botswana’s anti-sodomy laws as a “British import,” adding that they had been developed “without consultation of local peoples.”