How to use dynamic localization keys with SwiftUI - ios

I'm trying to display a localized text based on an ID. I have a lot of these, so it feels more efficient to just add dynamically the ID to the string.
The string file looks like this:
"users.1.name" = "Alice"
"users.2.name" = "Bob"
"users.3.name" = "Charles"
...
If I do the following, hardcoding the ID, it works as expected and displays the associated translated key:
Text("users.1.name")
However if I do this it only displays the string:
Text("users.\(user.id).name")
// displays "users.1.name" instead of "Alice"
I've also tried:
Text(LocalizedStringKey("users.\(user.id).name"))
// displays "users.1.name" instead of "Alice"
Am I missing something or is this not possible?

You need then the following. Tested with Xcode 11.4 / iOS 13.4
Text(NSLocalizedString("users.\(id).name", comment: ""))

I'm not a SwiftUI expert but you can use:
let key:String = "users.\(user.id).name"
Text(LocalizedStringKey(key))
// OR compact
Text(LocalizedStringKey(String("users.\(user.id).name")))

Related

XCode 9 issue. Failed to set user defined property in interface builder

My issue is that settings a keypath and value in interface builder does not seem to be working in XCode 9. There is a work around where you make the variable inspectable.
I've created a sample project and details below.
I created a simple test class where I set properties given a path and a value, like so.
You'll see there are two strings being set, ignore backgroundColor that was a sanity check, key path "string" works but key path "string2" does not. "string" And "string2" differ in one way and that's in their declaration as shown here.
#IBInspectable var string: String = ""
var string2: String = ""
The result of all this is code is shown here.
I used to do this all the time in XCode 8, ios9 without issue, did I miss something with the update? I have a work around but the issue still vexes me.
// mark the line as #objc
#objc var string: String
This was the answer given by dan in the comments.

Swift: read string based on provided user info

On Xcode (Swift) I want to load data stored on the app based on information provided by the user.
For example, the user gives the input of "Xcode":
var userInput = "Xcode"
With this information, I want to display the a stored string with the exact same name that is already on the app:
let Xcode = "Xcode is a development tool."
This is what I get when I print:
print ("Print: ", userInput) -> Print: Xcode
But I want to print the result from the string value stored from the app instead. The result I'm looking for is this:
print ("Print: ", userInput) -> Print: Xcode is a development tool.
I have to associate the input to the string stored, how can I do this without manually associating thousands of words to the strings I want to show? What is the best approach to get this result?
Thanks!
My approach where I have to do one by one is this:
switch userInput {
case Xcode: // This is the info provided by the user.
userInput = Xcode // This is the string stored on the app.
break
}
But once I have thousands of strings this approach is terrible, but works:
print ("Print: ", userInput) -> Print: Xcode is a development tool.
You have to define a data model for your information. If you have too much data, I suggest to create a JSON file where each key is the userInput with the associated value. Then you can parse the JSON file into a dictionary where you can easily retrieve the key-related value.
At this point you can define your own print method that prints the value associated with the key passed as parameter simply using object(forKey:) method.
I can't exactly understand your question. But what I've understood is to do this with Dictionary data type. Here is the sample code for this.
var str = "Hello, playground"
var responseMessages : [String : String] = ["" : ""]
if responseMessages[str] == nil
{
responseMessages[str] = str;
}
print(responseMessages[str]) // Output : Optional("Hello, playground")
You know better to remove optional keyword from string values by casting them to String data type.

How change app language in real time?

I am working on an app which runs in 2 languages, English and Persian (farsi). So user can select his desired language and app is displayed in that language. What should I do?
Depends on what are your trying to achieve, there is an approach that might be useful to your case, which is:
Declare a global variable which should represents the key of the current used language in the app, call it appLanguageKey String -for example-:
var appLanguageKey = "english"
Create a datastore for storing both languages desired caption for each term, it might looks like (contains):
term_key term_english term_farsi
greeting_key Hello Hoi
bye_key Bye Doei
For now, you could get the desired value if you tried to do:
desiredTerm = "select term_\(appLanguageKey) where term_key = 'greeting_key'"
Consider it as pseudo-code, the condition should be similar to it.
By implementing such a function that returns a string (I will assume that it is: getDesiredCaption(_ termKey: String) -> String in the following code snippet), you will be able to automatically set the desired caption for any UI component, just call it in viewWillAppear method the view controller:
override func viewWillAppear(_ animated: Bool) {
.
.
.
label.text = getDesiredCaption("greeting_key")
// Since that 'appLanguageKey' global viriable is "english",
// the output should be "Hello"
.
.
.
}
All you have to do for changing the language of the app is to change the value of appLanguageKey to "farsi".
For another approach which related to the Internationalization and Localization, you might want to check this answer, it should affect the app to let its navigation to be from right to left.
Hope this helped.
First you should use realm, magical records or an API to store what the user picks. Then you could create your own type of localizeable string handeler, by having an array with 2 dictionaries 1 with english and 1 with persian that all the texts in your application will read from just as you do with a normal localizeable.

Objective-C iOS parse string according to the definition

I want create an iOS app for my school.
This App will show Week schledule and when I tap on Cell with Subject, it will show me detail info about subject...
My problem:
Our teachers use shotcuts for their names, but I want show their full name... I created the file "ucitele.h" with definitions of their names, but I don't know, how to use it 😕.
This is how that file looks:
//
// ucitele.h
//
#define Li #"RNDr. Dan---vá"
#define He #"Mgr. Ja---hl"
#define Sm #"Ing. Mich---rek"
#define Ks #"Mgr. Svat---á"
I get the shortcut of Teacher from previous view from "self.ucitel" and I maybe want compare the contents of the "self.ucitel" with definitions and set the "ucitelFull" string from the definitions? I don't know how to say it 😕.
when the content of the self.ucitel will be #"Sm", than I want parse "ucitelFull" as #"Ing. Mich---rek"
Answers in Objective-C only please
Okay, sounds like your trying to map a short identifier to a full name:
-(NSString*)fullNameFromShortName:(NSString*)short {
NSDictionary * names = #{#"Li" : #"RNDr. Dan---vá",
#"He" : #"Mgr. Ja---hl", ... };
return [names objectForKey:short];
}
Use like:
self.ucitelFull = [self fullNameFromShortName:self.ucitel];
This is a dictionary that has the short name as a key and the full name as the value.
Some further suggestions:
try using lowercase keys and comparing lowercaseString's, incase the user doesn't enter the value with the correct case.
You can move the dictionary definition into a json file and read it from your bundle, to eliminate the hardcoding

How to force the my url not to change the value of '#'

I have an URL address that I change dynamically I it goes something like this:
The dynamic part is the -> edition = Model.Edition.Usually it as an integer value and the url ends up something like that: ....&edition=1232113 . Sometimes I need it to end up like that : &edition=1232113#10_11 and I managed to pass th right value to the edition placeholder but after the reload it doesn't show the same url that I expected it is similar but it substitutes the '#' with '%23'. And it looks something like that: 1232113%2310_11 and the effect is not what I expect.
From the other side, when I type it manually : 1232113#10_11 it works.
Can you help?
If you problem is concerning that the Url.Action is converting a part of your url, you may want to use the RAW method.
#Html.Raw(Url.Action("Method","Controller", new { Id = Model.DId, dbId = Model.DbId, iconId = Model.IconId, edition = Model.Edition })

Resources