By today, 22 people have been murdered in orchestrated attacks by groups of South Africans against immigrants in poor townships around Johannesburg. Two of these were burned to death. The victims are mainly Zimbabwean immigrants. News reports quote the attackers as saying the immigrants are "job stealers".

This is a modern, industrialised country, with one of the world's most progressive constitutions, that prides itself on inclusivity. South Africans champion such concepts as the "rainbow nation" and "the world in one country", and despite much resistance, held a much-heralded truth and reconciliation commission.

This makes the events of the last week even more vexing.

But South Africa has not confronted all its evil, evidently. The xenophobia that prompted these attacks permeates society.

This despite the fact that South Africa's powerhouse industries - gold mining and the manufacturing sectors of its industrial heartland of Johannesburg - were built on migrant labour, much of it from neighbouring countries, with populations that also paid a heavy prize for their governments' and people's support of the anti-apartheid struggle. They hosted political exiles and endured bombings, assassinations and military aggressions.

Although it does not justify it, the immediate cause for the violence of the last week is the desperation of sections of the poor black South Africans living in subhuman conditions. South Africa remains the most unequal country in the world. As polling firm Markinor (using very optimistic measures) reported earlier this month, in an increasingly youthful population (78% black), only 42 of every 100 South Africans have a job, 49% are poor (with monthly household income below R2,400 or £170), 13% are HIV positive, 24% of homes have no electricity, 32 % no tap water, 69% no hot water supply, and R21 (£1.40) of every R100 (£6.80) they earn, they spend on food.

Most of these people have consistently voted for the African National Congress of Nelson Mandela and Thabo Mbeki, and next year they will probably make Jacob Zuma South Africa's next president. What they have witnessed under an ANC government, however, is how a small elite have enriched themselves, how whites have actually benefited from freedom and how the majority still lives in poverty with high rates of violence and illness.

However, as Zwelinzima Vavi, leader of the country's largest trade union federation, Cosatu, noted this week, the poor and desperate are not about to revolt, but instead will turn against foreigners.

Vavi's comment has a political subtext. Cosatu has been a consistent critic of an Mbeki administration it sees as insufficiently committed to labour and redistribution. But he has a point. Worse, the bulk of anti-foreigner sentiment is aimed at black Africans. The violence of last week is also not a new phenomenon. The last decade has witnessed attacks on Somali street traders in Cape Town, people thrown off trains outside Johannesburg and immigrants rounded up on the streets (including South Africans deemed "too dark"), held in cramped, inhumane detention centres and put on overnight trains out of the country.

Poverty and desperation are only part of the story.

Extensive research (pdf) by the Southern African Migration Project (Samp) has shown that South Africa, Botswana and Namibia, are among the most xenophobic countries in the world and that South Africans hold by far the harshest anti-immigrant sentiments. Furthermore, these anti-immigrant and anti-refugee sentiments cut across all major socio-economic and demographic categories. Young and old, black and white, educated or not. They "display an extraordinary consistency in their antagonism towards foreigners, particularly those from other countries in Africa and especially those deemed to be 'illegal immigrants'." Even refugees are viewed negatively.

South Africa and the neighbouring countries largely shaped by its policies have always been about hating others. Colonialism and apartheid (the two systems that dominated its 400 year history) were built on such a consciousness. Pundits and observers of South Africa often generalise about its progressive politics. What they forget is that sections of South Africa's political class - a small minority - leads its population to adopt progressive laws and attitudes on sexuality, marriage, capital punishment and even immigration. In contrast, the population is generally conservative and socially right-wing. Openness and tolerance and a historical consciousness did not necessarily go along with opposing apartheid.

Most South Africans don't have passports and rarely travel into the rest of the continent. Of those who travel, mostly whites, they go to Europe (which they culturally identify with), Australasia and North America. The education system, even after apartheid, has not done much to improve this state of affairs. It's a hard fact to come to terms with, but Thabo Mbeki's African Renaissance failed largely because it did not connect to the country's black majority. In the same way, while laudable, the recent solidarity action by Durban dockworkers and truckers, which involved them refusing to unload and then transport guns to the Zimbabwean regime, is an exception.

An interesting finding of Samp has been that anti-immigrant sentiments exist in South Africa despite relatively little direct contact with people from other countries. Less than 10% of survey respondents have had a "great deal" of contact with people from other countries, 35% said they had "some contact", 11% said they had "hardly any" contact and a remarkable 43% said they have had "no contact at all" with immigrants.

The misinformation and sentiments about foreigners come from elsewhere: the public utterances and collusion by political leaders and public officials (police, municipal officials) and more importantly from media images. In a research study by Samp that I worked on, we found that South African media coverage of foreigners in a wide range of sources (from television news documentaries, broadsheets to tabloids) are overwhelmingly negative, relying on stereotypes about foreigners as "criminals", "illegals" and "job stealers". Evidence for such stereotypes was never substantiated by evidence. While some of the coverage got better over time, stereotypes persist.

Some commentators in South Africa have blamed the current wave of xenophobic violence on the crisis in Zimbabwe (that large numbers of Zimbabweans fleeing Mugabe's terror add to job and crime woes) or suggested that the instigators are not South African, as Mandela's ex-wife Winnie Mandela did last week. It would be an inadequate response if it were true. And it is not true. South Africa has to face up to some hard truths this week.