Rashi explains, quoting Yoma 11a: “And on your gates – this includes the gates of courtyards, the gates of provinces and the gates of cities”. Hence any city or country surrounded by a wall needs a mezuzah.
Its purpose is suggested by these words of Edmund Burke: “The moral law was not written for men alone in their individual character, but it was written as well for nations, and for nations great as this of which we are citizens.
“If nations reject and deride that moral law, there is a penalty which will inevitably follow. We have the unchangeable and eternal principles of the moral law to guide us, and only so far as we walk by that guidance can we be permanently a great nation, or our people a happy people.”
Judaism enshrines this principle in the law that nations must write the Shema on their doorposts and inspect their mezuzot twice in seven years in order to assess the moral quality of their national life.
It is unlikely that any nation can give itself a clean bill of health; there are blotches on the face of every people. But would it not be a wonderful idea for nations to have regular occasions (say, twice in every seven year cycle) to look at themselves in the mirror?