Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solve SPM localization issue #145

Merged
merged 4 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mantis.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/guoyingtao/Mantis.git", :tag => "#{s.version}" }
s.source_files = "Sources/**/*.{h,swift}"
s.resource_bundles = {
"MantisResource" => ["Sources/**/*.lproj/*.strings"]
"MantisResources" => ["Sources/**/*.lproj/*.strings"]
}

s.pod_target_xcconfig = {
Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PackageDescription

let package = Package(
name: "Mantis",
defaultLocalization: "en",
platforms: [.iOS(.v11), .macOS(.v10_15)],
products: [
.library(
Expand All @@ -14,6 +15,9 @@ let package = Package(
targets: [
.target(
name: "Mantis",
dependencies: [])
exclude: ["Resources/Info.plist"],
resources: [.process("Resources")],
swiftSettings: [.define("MANTIS_SPM")]
)
]
)
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ Please use the transformation infomation obtained previously from delegate metho
</p>

* SwiftUI project
please check this [link](https://github.com/guoyingtao/Mantis/discussions/123#discussioncomment-1127611)
Please check this [link](https://github.com/guoyingtao/Mantis/discussions/123#discussioncomment-1127611)

* Static frameworks
If you use static frameworks in CocoaPods, you need to add the code below in order to find the correct resource bundle.

```
Mantis.locateResourceBundle(by: Self.self)
```

* Custom localization tables and bundle

Expand Down
16 changes: 14 additions & 2 deletions Sources/Mantis/Helpers/LocalizedHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct LocalizedHelper {

static func setBundle(_ bundle: Bundle) {
guard let resourceBundleURL = bundle.url(
forResource: "MantisResource", withExtension: "bundle")
forResource: "MantisResources", withExtension: "bundle")
else { return }
LocalizedHelper.bundle = Bundle(url: resourceBundleURL)
}
Expand All @@ -25,16 +25,28 @@ struct LocalizedHelper {
) -> String {
let value = value ?? key

#if MANTIS_SPM
let bundle = localizationConfig.bundle ?? Bundle.module

return NSLocalizedString(
key,
tableName: localizationConfig.tableName,
bundle: bundle,
value: value,
comment: ""
)
#else
guard let bundle = LocalizedHelper.bundle ?? (localizationConfig.bundle ?? Mantis.bundle) else {
return value
}

return NSLocalizedString(
key,
tableName: localizationConfig.tableName,
bundle: bundle,
value: value,
comment: ""
)
#endif
}
}
2 changes: 1 addition & 1 deletion Sources/Mantis/Mantis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public struct Config {
return nil
}

if let url = bundle.url(forResource: "MantisResource", withExtension: "bundle") {
if let url = bundle.url(forResource: "MantisResources", withExtension: "bundle") {
let bundle = Bundle(url: url)
return bundle
}
Expand Down