kiwix-apple/Support/StringLocalExtension.swift_temp
2025-01-26 10:51:05 +01:00

57 lines
1.9 KiB
Plaintext

// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.
import Foundation
fileprivate extension String {
var localized: String {
localizedWithFallback()
}
func localizedWithFormat(withArgs: CVarArg...) -> String {
let format = localizedWithFallback()
switch withArgs.count {
case 1: return String.localizedStringWithFormat(format, withArgs[0])
case 2: return String.localizedStringWithFormat(format, withArgs[0], withArgs[1])
default: return String.localizedStringWithFormat(format, withArgs)
}
}
private func localizedWithFallback(
bundle: Bundle = Bundle.main,
comment: String = ""
) -> String {
let englishValue: String
if let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
let bundle = Bundle(path: path) {
englishValue = NSLocalizedString(self, bundle: bundle, comment: comment)
if NSLocale.preferredLanguages.first == "en" {
return englishValue
}
} else {
englishValue = ""
}
return NSLocalizedString(
self,
tableName: nil,
bundle: bundle,
value: englishValue, // fall back to this, if translation not found
comment: comment
)
}
}