iink SDK on Device

Answered

How to dynamically change language for iOS/Swift project

Hi,

I'm using the MyScript SDK and following your sample projects, I've used your reference UI library to create UI in my app. Now I want to add ability to change language during runtime, say from English to German. Could you help me with code sample in Swift? I've already looked at this link, but I'm unable to achieve same in Swift.

Another question, will all the text written in previously language need to be removed to change language?

Regards,

Vaibhav


Best Answer

Dear Vaibhav,

thank you for contacting us.

When you say you are unable to achieve the same in Swift, which difficulty are you facing?

Indeed, the code you pointed should not be that difficult to convert from JAVA to Swift. The main diffculty I see is when calling the "setString" function, the key and value are inverted:
In JAVA: conf.setString("text.configuration.name","text");
Will become in Swift: editorViewController.editor.configuration.setString("text" forKey: "text.configuration.name");.

Another question, will all the text written in previously language need to be removed to change language?
>>The strokes that were already converted will be kept in the previous language. You will not be able to convert these in the new language "as-is". If you want to do this, you shall keep the strokes, clear the part, and add back the strokes: https://developer.myscript.com/docs/interactive-ink/1.3/ios/fundamentals/editing/#incremental-input

Let us know if you have further questions.

Best regards,

Olivier



Answer

Dear Vaibhav,

thank you for contacting us.

When you say you are unable to achieve the same in Swift, which difficulty are you facing?

Indeed, the code you pointed should not be that difficult to convert from JAVA to Swift. The main diffculty I see is when calling the "setString" function, the key and value are inverted:
In JAVA: conf.setString("text.configuration.name","text");
Will become in Swift: editorViewController.editor.configuration.setString("text" forKey: "text.configuration.name");.

Another question, will all the text written in previously language need to be removed to change language?
>>The strokes that were already converted will be kept in the previous language. You will not be able to convert these in the new language "as-is". If you want to do this, you shall keep the strokes, clear the part, and add back the strokes: https://developer.myscript.com/docs/interactive-ink/1.3/ios/fundamentals/editing/#incremental-input

Let us know if you have further questions.

Best regards,

Olivier


Dear Oliver,

Thank you for your quick reply. You were right, the code when translated to Swift worked. I had made mistake in assigning value for "text.configuration.bundle".

The code I have now is something like below: 

@IBAction func inputTypeSegmentedControlValueChanged(_ sender: UISegmentedControl) {
        
        //0-English 1-German
        let language = sender.selectedSegmentIndex == 0 ? "en_US" : "de_DE"
        do {
            try editorViewController.editor.configuration.setString(language,
                                                                    forKey: "lang")
            try editorViewController.editor.configuration.setString(language,
                                                                    forKey: "text.configuration.bundle")
            try editorViewController.editor.configuration.setString("text",
                                                                    forKey: "text.configuration.name")
        } catch {
            print("Failed to configure language to \(language)")
        }
        
        if editorViewController.editor.part != nil {
            editorViewController.editor.clear()
            editorViewController.editor.part = nil
        }
        do {
            try editorViewController.editor.part = contentPackage?.getPartAt(0)
            
            if editorViewController.editor.part != nil {
                editorViewController.editor.clear()
                editorViewController.editor.part = nil
            }
            editorViewController.editor.part = nil
            editorViewController.editor.clear()
            editorViewController.editor.part = try contentPackage?.createPart("Text")
        }
        catch {
            print("Error while creating package : " + error.localizedDescription)
        }
    }

It seems to be working fine. I hope I'm not doing anything wrong here.

Regards,

Vaibhav 

Dear Vaibhav,


thank your for the update, I am glad it is working.


Having a quick look to your code, it indeed looks fine.


Feel free to contact us if you have any other question.


Best regards,


Olivier