Treatment of magnesium or alloys based thereon