An iOS framework for quick multi-language support using Google Translate API. Strings can be translated directly from a .strings file, or passed in manually. An API key is required, and clients will be charged only after reaching a freemium threshold. For more information, check out Google Translate API. This framework utilizes caching to limit API calls.
bsg-translator-demo.mp4
- iOS 13+
- Google Translate API
- Create a RapidAPI account.
- Visit API Hub from the dashboard and subscribe to Google Translate API.
- Navigate to My Apps->Add New App from the dashboard and create a new application.
- Copy the API key for the new application.
- Navigate to File->Add Packages.
- Enter Package URL: https://github.com/brook-street-games/bsg-translator.git
- Select a dependency rule. Up to Next Major is recommended.
- Select a project.
- Select Add Package.
Create a new file at File->New->File->Strings File. This file should contain keys and input language translations for all text to be translated.
Sample.strings
"apple" = "apple";
"banana" = "banana";
"blueberry" = "blueberry";
"coconut" = "coconut";
"cherry" = "cherry";
"grape" = "grape";
"kiwi" = "kiwi";
"lemon" = "lemon";
"orange" = "orange";
"peach" = "peach";
"pear" = "pear";
"pineapple" = "pineapple";
"strawberry" = "strawberry";
"watermelon" = "watermelon";
// Import the framework.
import BSGTranslator
// Grab the API key from installation.
let apiKey = "API_KEY"
// Create an instance of Translator.
let translator = Translator(apiKey: apiKey, inputLanguage: "en", inputType: .stringsFile(fileName: "Sample"))
// Add a delegate.
translator.delegate = self
// Translate input strings into output language (defaults to iOS settings).
translator.updateTranslations()
// Handle the result.
func translator(_ translator: Translator, didUpdateTranslations result: Result<TranslationSet, TranslationError>) {
switch result {
case .success:
// Handle completion.
case .failure(let error):
// Handle error.
}
}
label.text = translator.translate("pineapple")
A translation ID can be used to force a new API call when string values change. For example, if an application is being translated to Italian, and the english translation for pineapple was changed from "pineapple" to "spikey fruit" in a new version, users with the previous version would still return the cached value "ananas". Passing a translation ID of 2 (assuming nil or a lower value was previously passed) would force a translation and correctly return "frutto appuntito".
translator.updateTranslations(translationId: 2)
Log events provide more information about what is going on under the hood of translator..
func translator(_ translator: Translator, didEncounterLogEvent event: String) {
// Handle log event.
}
- None. No adjustment.
- First. Capitalize the first letter of the first word.
- All First. Captitalize the first letter of each word.
- All. Capitalize all letters.
label.text = translator.translate("pineapple", capitalization: .all)
Brook Street Games LLC