Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mac-cain13/R.swift.Library
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: kemchenj/R.swift.Library
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 3 commits
  • 2 files changed
  • 1 contributor

Commits on Mar 11, 2021

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    183ccae View commit details
  2. Add value to StringResource

    kemchenj committed Mar 11, 2021
    Copy the full SHA
    0831324 View commit details
  3. Copy the full SHA
    a36b167 View commit details
Showing with 110 additions and 3 deletions.
  1. +87 −3 Library/Core/StringResource.swift
  2. +23 −0 Library/UIKit/ImageResource+UIKit.swift
90 changes: 87 additions & 3 deletions Library/Core/StringResource.swift
Original file line number Diff line number Diff line change
@@ -22,7 +22,10 @@ public protocol StringResourceType {

/// Locales of the a localizable string
var locales: [String] { get }


/// The value to return if key is nil or if a localized string for key can’t be found in the table.
var value: String? { get }

/// Comment directly before and/or after the string, if any
var comment: String? { get }
}
@@ -40,15 +43,96 @@ public struct StringResource: StringResourceType {

/// Locales of the a localizable string
public let locales: [String]


/// The value to return if key is nil or if a localized string for key can’t be found in the table.
public let value: String?

/// Comment directly before and/or after the string, if any
public let comment: String?

public init(key: String, tableName: String, bundle: Bundle, locales: [String], comment: String?) {
public init(key: String, tableName: String, bundle: Bundle, locales: [String], value: String?, comment: String?) {
self.key = key
self.tableName = tableName
self.bundle = bundle
self.locales = locales
self.value = value
self.comment = comment
}

public func callAsFunction(preferredLanguages: [String]? = nil) -> String {
guard let preferredLanguages = preferredLanguages else {
return NSLocalizedString(key, bundle: bundle, value: value ?? "", comment: comment ?? "")
}

guard let (_, bundle) = localeBundle(tableName: tableName, preferredLanguages: preferredLanguages) else {
return value ?? ""
}

return NSLocalizedString(key, bundle: bundle, value: "", comment: comment ?? "")
}

private var applicationLocale: Locale {
bundle.preferredLocalizations.first.flatMap { Locale(identifier: $0) } ?? Locale.current
}

private func localeBundle(tableName: String, preferredLanguages: [String]) -> (Foundation.Locale, Foundation.Bundle)? {
// Filter preferredLanguages to localizations, use first locale
var languages = preferredLanguages
.map { Locale(identifier: $0) }
.prefix(1)
.flatMap { locale -> [String] in
if bundle.localizations.contains(locale.identifier) {
if let language = locale.languageCode, bundle.localizations.contains(language) {
return [locale.identifier, language]
} else {
return [locale.identifier]
}
} else if let language = locale.languageCode, bundle.localizations.contains(language) {
return [language]
} else {
return []
}
}

// If there's no languages, use development language as backstop
if languages.isEmpty {
if let developmentLocalization = bundle.developmentLocalization {
languages = [developmentLocalization]
}
} else {
// Insert Base as second item (between locale identifier and languageCode)
languages.insert("Base", at: 1)

// Add development language as backstop
if let developmentLocalization = bundle.developmentLocalization {
languages.append(developmentLocalization)
}
}

// Find first language for which table exists
// Note: key might not exist in chosen language (in that case, key will be shown)
for language in languages {
if let lproj = bundle.url(forResource: language, withExtension: "lproj"),
let lbundle = Bundle(url: lproj)
{
let strings = lbundle.url(forResource: tableName, withExtension: "strings")
let stringsdict = lbundle.url(forResource: tableName, withExtension: "stringsdict")

if strings != nil || stringsdict != nil {
return (Locale(identifier: language), lbundle)
}
}
}

// If table is available in main bundle, don't look for localized resources
let strings = bundle.url(forResource: tableName, withExtension: "strings", subdirectory: nil, localization: nil)
let stringsdict = bundle.url(forResource: tableName, withExtension: "stringsdict", subdirectory: nil, localization: nil)

if strings != nil || stringsdict != nil {
return (applicationLocale, bundle)
}

// If table is not found for requested languages, key will be shown
return nil
}
}
23 changes: 23 additions & 0 deletions Library/UIKit/ImageResource+UIKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ImageResource+UIKit.swift
// R.swift.Library
//
// Created by kemchenj on 03-11-21.
// From: https://github.com/mac-cain13/R.swift.Library
// License: MIT License
//

#if os(iOS) || os(tvOS)
import UIKit

extension ImageResource {
/// Returns the image from this resource (R.image.*) that is compatible with the trait collection.
///
/// - Parameter traitCollection: The traits associated with the intended environment for the image. Use this parameter to ensure that the correct variant of the image is loaded. If you specify nil, this method uses the traits associated with the main screen.
/// - Returns: Traits that describe the desired image to retrieve, pass nil to use traits that describe the main screen.
public func callAsFunction(compatibleWith traitCollection: UITraitCollection? = nil) -> UIImage? {
UIImage(resource: self, compatibleWith: traitCollection)
}
}
#endif