A robust and efficient frequency dependent and nonlocal exchange correlation f(xc)(r,r(');omega) is derived by imposing time-dependent density-functional theory (TDDFT) to reproduce the many-body diagrammatic expansion of the Bethe-Salpeter polarization function. As an illustration, we compute the optical spectra of LiF, SiO2, and diamond and the finite momentum transfer energy-loss spectrum of LiF. The TDDFT results reproduce extremely well the excitonic effects embodied in the Bethe-Salpeter approach, both for strongly bound and resonant excitons. We provide a working expression for f(xc) that is fast to evaluate and easy to implement.