Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
R.swift extension to support in-app switch
//
// 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