I want to implement dynamic localization inside my app. I use Firestore to store json. Also, I want to be able to manually select language inside my app.
To do that, I use Bundle
with path for specific lang like this:
let innerPath = internalCustomBundle.path(forResource: "en", ofType: "lproj")
let innerBundle = Bundle(path: innerPath)!
The problem is, that if I use internalCustomBundle
, then I have up-to-date string synced with server. But at the same, innerBundle
always return an old one, firstly initializated remote string.
So the steps looks like this:
- Launch the app.
localizedString
from internalCustomBundle and innerBundle are the same.- Go to server and update the string.
- String from
internalCustomBundle
up-to-date new string, innerBundle old string. (I haveaddSnapshotListener
to listen for updates).
General bundle (internalCustomBundle):
NSBundle </var/mobile/Containers/Data/Application/27E2BF13-B415-46D3-ADD8-0DE9A7F0D9E2/Documents/DynamicLocalization> (not yet loaded)
Language specific bundle (innerBundle):
NSBundle </var/mobile/Containers/Data/Application/27E2BF13-B415-46D3-ADD8-0DE9A7F0D9E2/Documents/DynamicLocalization/en.lproj> (not yet loaded)
Is it an Apple bug? By the way, I only have en
.
Write to bundle method:
if !fileManager.fileExists(atPath: bundlePath.path) {
try fileManager.createDirectory(
at: bundlePath,
withIntermediateDirectories: true,
attributes: [:]
)
}
let languages = json.dictionaryValue
for language in languages.keys {
let langPath = bundlePath.appendingPathComponent("\(language).lproj", isDirectory: true)
if !fileManager.fileExists(atPath: langPath.path) {
try fileManager.createDirectory(
at: langPath,
withIntermediateDirectories: true,
attributes: [:]
)
}
let sentences = json[language].dictionaryValue.reduce("", {
$0 + "\"\($1.key)\" = \"\($1.value.stringValue)\";\n"
})
let filePath = langPath.appendingPathComponent("Localizable.strings")
let data = sentences.data(using: .utf32)
fileManager.createFile(
atPath: filePath.path,
contents: data,
attributes: [:]
)
}