How to best handle this localization scenario?

Hello,
here is a particular localization scenario i'm not sure how to handle with default localization feature:

App base language is english, translation is french.

1) On app load, swift will pick up 6 random items from a strings array of 100 items.
2) It will display each of those 6 strings on 6 user interface buttons.
3) User will tap on a button: string value is sent back into app.
Here is the problem:
4) App will do processing with this value. Problem is that all internal processing from that values that went out from the array on user interface then back in app, is all done with english values. In particular that user choice is saved in external database, and I can't have saved in database localized versions: all internal processing on array values in english.

How best to handle this situation?
Is it possible from a localized french VALUE, to get corresponding VALUE in base language?

If yes how?

Or should I build my own thing for those cases when random values goes out on UI, then back in app... like an array of tuples containing translated items?
Answered by Claude31 in 674187022

Is it possible from a localized french VALUE, to get corresponding VALUE in base language?

You may have another way (I do not recommend, but it will require less change to your code).

When you get the answer (in French or whatever language), build the array of localized strings from original array
Code Block
let localizedStrings = originalArray.map() { NSLocalizedString($0, comment: "") }

Search for the answer and get the base string

Code Block
var baseAnswer = ""
if let index = localizedStrings.firstIndex(of: answer) {
baseAnswer = originalArray[index]
}


Do what you need with baseAnswer
So, you send back the button title probably, which is effectively localized

Did you consider using buttons tags ?
I would do this:

1) On app load, swift will pick up 6 random items from a strings array of 100 items.
keep track of the index of each item selected

2) It will display each of those 6 strings on 6 user interface buttons.
Set the button tag with the index you have used to set its title.

3) User will tap on a button: string value is sent back into app.
send back the tag value instead.

4) App will do processing with this value. Problem is that all internal processing from that values that went out from the array on user interface then back in app, is all done with english values. In particular that user choice is saved in external database, and I can't have saved in database localized versions: all internal processing on array values in english.
To do the processing, get back the string value from the array, using the index you received.

If there are still problem, please explain.
Otherwise, don't forget to close the thread on this answer.

Goos continuation.
Thank you very much for your answer. That seems a smart way to resolve the problem. I will test this solution over the next few days, and will not forget to come back and close the thread if all is fine
Accepted Answer

Is it possible from a localized french VALUE, to get corresponding VALUE in base language?

You may have another way (I do not recommend, but it will require less change to your code).

When you get the answer (in French or whatever language), build the array of localized strings from original array
Code Block
let localizedStrings = originalArray.map() { NSLocalizedString($0, comment: "") }

Search for the answer and get the base string

Code Block
var baseAnswer = ""
if let index = localizedStrings.firstIndex(of: answer) {
baseAnswer = originalArray[index]
}


Do what you need with baseAnswer
Thanks for the second solution.
I'm giving it a go.

I'll create the localizedStrings once for all on app initialization, then create a string extension "ToBaseLang" to handle all the rest.
Creating a String extension is a nice and swifty way to do this.

Good continuation.
How to best handle this localization scenario?
 
 
Q