Force localization of the allowedUnits in DateComponentsFormatter
<p>I have an app with only one language. I would like to force the language to be this when displaying a time interval since a date. E.g. "9 mths ago" Just in a specific language not english. Is there a way to do this using DateComponentsFormatter which otherwise does exactly what I want to do with the units.</p>

<pre><code>    func format(duration: TimeInterval) -&gt; String {
        UserDefaults.standard.set(["da"], forKey: "AppleLanguages")
        UserDefaults.standard.synchronize()
        let formatter = DateComponentsFormatter()
        formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]
        formatter.unitsStyle = .short
        formatter.includesApproximationPhrase = true
        formatter.maximumUnitCount = 1

        return formatter.string(from: -duration)!
    }

    format(duration: self.createdDate.timeIntervalSinceNow)
</code></pre>

<p>This returns e.g. "1 yr" when language is en.
I would like to have it in a specific language ("yr" -> "år") this should be similarly done for the other allowed units.</p>

<p>It works when the phone is set to the language of the app.</p>

<p>I tried to set the language before creating the DateComponentsFormatter but it still returned in the language the phone is set to. </p>

<p><strong>EDIT inserted working code below</strong></p>

<p>Working code after modification from answer:</p>

<pre><code>    func format(duration: TimeInterval) -&gt; String {
        var calendar = Calendar.current
        calendar.locale = Locale(identifier: "da")
        let formatter = DateComponentsFormatter()
        formatter.calendar = calendar
        formatter.allowedUnits = [.year, .month, .day, .hour, .minute, .second]
        formatter.unitsStyle = .short
        formatter.includesApproximationPhrase = true
        formatter.maximumUnitCount = 1

        return formatter.string(from: -duration)!
    }

    format(duration: self.createdDate.timeIntervalSinceNow)
</code></pre>