We introduce a functional for simulating ground-state and time-dependent electronic systems within density-functional theory. The functional combines an expression for the exact Kohn-Sham (KS) potential in the limit of complete electron localization with a measure of the actual localization. We find accurate self-consistent charge densities, even for systems where the exact exchange-correlation potential exhibits nonlocal dependence on the density, such as potential steps. We compare our results to the exact KS potential for each system. The self-interaction correction is accurately described, avoiding the need for orbital-dependent potentials.