Created
June 23, 2020 14:41
-
-
Save jhonny-me/dd2edb43fb167d1f55f79021718507b7 to your computer and use it in GitHub Desktop.
R.swift extension to support in-app switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StringResource+Localization.swift | |
// Core | |
// | |
// Created by Johnny Gu on 2020/6/22. | |
// | |
import Foundation | |
import Rswift | |
public extension StringResource { | |
static var currentLanguage: String = "en" | |
func localized(_ language: String = currentLanguage) -> String { | |
guard | |
let basePath = bundle.path(forResource: "en", ofType: "lproj"), | |
let baseBundle = Bundle(path: basePath) | |
else { | |
return self.key | |
} | |
let fallback = baseBundle.localizedString(forKey: key, value: key, table: tableName) | |
guard | |
let localizedPath = bundle.path(forResource: language, ofType: "lproj"), | |
let localizedBundle = Bundle(path: localizedPath) | |
else { | |
return fallback | |
} | |
return localizedBundle.localizedString(forKey: key, value: fallback, table: tableName) | |
} | |
} | |
public extension String { | |
func withInputs(_ params: CVarArg...) -> String { | |
return String(format: self, arguments: params) | |
} | |
} | |
// Usage: R.string.localizable.alert_ok.localized() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment