It happens through a random event--but not quite as random as others. It used to be quite random (in BtS, not at all random before), but now it is suppose to happen every 100 turns (at normal speed, adjusted for the game speed). Still, I don't think it will ever happen if you chose the option to turn off random events.
Also, you don't get a choice of any trait you want. You are only allowed to change one of your traits (I believe the permanent trait is listed first, so the second trait is the one you can switch), and the list of replacement traits is not all inclusive. You cannot, for example, choose the Agnostic, Insane, Adaptive, or Barbarian trait.
Even if you could, you would not want to switch to the barbarian trait. The actual advantage of the trait (peace with the barbs) isn't really a function of the trait itself, just how your diplomacy is set when you start the game. I seriously doubt that game keeps updating whether you should be at peace with them because of having the barb trait; after all, if you get too strong the barbs will declare war on you, and there is no way to go back to having peace. The other reason why you wouldn't want the trait is that even after you lose peace with the barbs, you are stuck with -10% research.