Skip to content

Instantly share code, notes, and snippets.

@jhonny-me
Created June 23, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhonny-me/dd2edb43fb167d1f55f79021718507b7 to your computer and use it in GitHub Desktop.
Save jhonny-me/dd2edb43fb167d1f55f79021718507b7 to your computer and use it in GitHub Desktop.
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