A general homogenization result for almost periodic functionals