A simple swift package for localizing your apps from JSON files with easy plural and dictionary handling.
JSONlize.shared.load(
fileHandler: { language in
//language: current system language string like "en-US"
return Bundle.main.url(forResource: "lang.\(language)", withExtension: "json") //returning an URL for file e.g. "lang.en-US.json"
},
defaultLanguage: "en-US" //fallback language if json file for current system language is not found
)Load a file with the load(fileHandler: Filehandler, defaultLanguage: String) function.
Filehandler: is a block that passes the current preferred system language as String for example "de-DE".
The block can return an optional object of type URL to the desired JSON file. If nil is returned, JSONLocalize calls the Filehandler block again for the default language set. In this Example "en-US".
defaultLanguage: The language to fallback to, if the file for the desired language is not found.
There are 3 different kinds of Strings that can be localized with JSONLize.LocalizedType.
enum LocalizedType {
case string
case plural(_ input: Int)
case dict(_ key: String)
}Following examples use these JSON example files:
{
"sentence": "This is a testsentence.",
"category": {
"keys": {
"travel": "Travel",
"art": "Art",
"food": "Food"
}
},
"comment": {
"plurals": {
"1": "Comment",
"*": "Comments"
}
},
"commentCount": {
"plurals": {
"1": "${i} Comment",
"*": "${i} Comments"
}
}
}{
"sentence": "Das ist ein Satz.",
"category": {
"keys": {
"travel": "Reisen",
"art": "Kunst",
"food": "Essen"
}
},
"comment": {
"plurals": {
"1": "Kommentar",
"*": "Kommentare"
}
},
"commentCount": {
"plurals": {
"1": "${i} Kommentar",
"*": "${i} Kommentare"
}
}
}use .JSONlized on any String to get the localized value
//en-US
"sentence".JSONlized //Output: "This is a testsentence."
//de-DE
"sentence".JSONlized //Output: "Das ist ein Satz."alternatively you can use .JSONlized(.string) to achieve the same result.
use JSONlized(_ localizedType: JSONlize.LocalizedType) with .dict as JSONlize.LocalizedType on any String to get the localized value of a dictionary value.
.dict(_ key: String) takes a String as parameter for the key to look up.
//en-US
"category".JSONlized(.dict("travel")) //Output: "Travel"
"category".JSONlized(.dict("art")) //Output: "Art"
"category".JSONlized(.dict("food")) //Output: "Food"
//de-DE
"category".JSONlized(.dict("travel")) //Output: "Reisen"
"category".JSONlized(.dict("art")) //Output: "Kunst"
"category".JSONlized(.dict("food")) //Output: "Essen"use JSONlized(_ localizedType: JSONLocalize.LocalizedType) with .plural as JSONLocalize.LocalizedType on any String to get the localized value of a plural value.
.plural(_ input: Int) takes an Int as parameter for the key to look up.
//en-US
"comment".JSONlized(.plural(0)) //Output: "Comments"
"comment".JSONlized(.plural(1)) //Output: "Comment"
"comment".JSONlized(.plural(2)) //Output: "Comments"
//de-DE
"comment".JSONlized(.plural(0)) //Output: "Kommentare"
"comment".JSONlized(.plural(1)) //Output: "Kommentar"
"comment".JSONlized(.plural(2)) //Output: "Kommentare"In a JSON plural definition, use the key "*" for all other cases.
JSON:
"comment": {
"plurals": {
"1": "Comment",
"*": "Comments"
}
}Swift:
//en-US
"comment".JSONlized(.plural(0)) //Output: "Comments" value of "*"
"comment".JSONlized(.plural(1)) //Output: "Comment" value of "1"
"comment".JSONlized(.plural(2)) //Output: "Comments" value of "*"Its possible to define ${i} in your plural values, which will be replaced by the input value.
JSON:
"commentCount": {
"plurals": {
"1": "${i} Comment",
"*": "${i} Comments"
}
}Swift:
"commentCount".JSONlized(.plural(0)) //Output: "0 Comments" replaced ${i} with 0
"commentCount".JSONlized(.plural(1)) //Output: "1 Comment" replaced ${i} with 1
"commentCount".JSONlized(.plural(2)) //Output: "2 Comments" replaced ${i} with 2MIT License
Copyright (c) 2020 Ohsome
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.