Skip to content

Commit

Permalink
Create localized language provider
Browse files Browse the repository at this point in the history
  • Loading branch information
adamnagydev committed Sep 3, 2024
1 parent e6b6011 commit 3a00163
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.build
/Packages
.DS_Store
xcuserdata
project.xcworkspace
36 changes: 35 additions & 1 deletion Sources/KeyboardShortcuts/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,47 @@
import Carbon.HIToolbox
import SwiftUI

public final class LocalizationLanguageProvider {
/// Provides the currently used language of the
/// app used to localize with.
public static var language: String {
Self.shared._language
}

/// Override translation language.
/// Useful if you manually wish to set the
/// language for localizations.
public static func override(_ language: String) {
Self.shared._language = language
}

/// Reset the language to default.
public static func reset() {
override(Self.default)
}

// MARK: Private

private init() {}
private static let shared = LocalizationLanguageProvider()
private static var `default`: String { Locale.autoupdatingCurrent.languageCode ?? "en" }
private var _language: String = LocalizationLanguageProvider.default
}

extension String {
/**
Makes the string localizable.
*/
var localized: String {
NSLocalizedString(self, bundle: .module, comment: self)
let language = LocalizationLanguageProvider.language
guard
let bundlePath = Bundle.module.path(forResource: language, ofType: "lproj"),
let bundle = Bundle(path: bundlePath)
else {
return NSLocalizedString(self, bundle: .module, comment: self)
}

return NSLocalizedString(self, bundle: bundle, comment: self)
}
}

Expand Down

0 comments on commit 3a00163

Please sign in to comment.