In the presence of a (time-dependent) macroscopic electric field the electron dynamics of dielectrics cannot be described by the time-dependent density only. We present a real-time formalism that has the density and the macroscopic polarization P as key quantities. We show that a simple local function of P already captures long-range correlation in linear and nonlinear optical response functions. Specifically, after detailing the numerical implementation, we examine the optical absorption, the second- and third-harmonic generation of bulk Si, GaAs, AlAs and CdTe at different level of approximation. We highlight links with ultranonlocal exchange-correlation functional approximations proposed within linear response time-dependent density functional theory framework.