What do NSLocalizedString() parameters "value" and "tableName" do? - ios

The Apple docs for this (both in Xcode and webpage) have exactly no explanation of the parameters.
https://developer.apple.com/documentation/foundation/1418095-nslocalizedstring
For reference, the function signature is
NSLocalizedString(
_ key : String,
tableName: String? = default, // ??
bundle : Bundle = default,
value : String = default, // ????
comment : String
) -> String
I have a vague idea what the tableName is -- but more information would be helpful. (Is it merely the filename for a strings file?) I have no idea what value is for.

The Objective-C documentation for NSLocalizedStringWithDefaultValue explains the parameters:
Parameters
key
The key for a string in the specified table.
tableName
The name of the table containing the key-value pairs. Also, the suffix for the strings file (a file with the .strings extension) to store the localized string.
bundle
The bundle containing the strings file.
value
The value to return if key is nil or if a localized string for key can’t be found in the table.
comment
The comment to place above the key-value pair in the strings file.
Basically, the key is looked up in a file named tableName.strings in the specified bundle. That strings file will have the format:
# comment
"key" = "value"

Related

How do you get strip RTF formatting and get actual string value using DXL in DOORS?

I am trying to get the values in "ID" column of DOORS and I am currently doing this
string ostr=richtext_identifier(o)
When I try to print ostr, in some modules I get just the ID(which is what I want). But in other modules I will get values like "{\rtf1\ansi\ansicpg1256\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\froman\fcharset0 Times New Roman;}} {*\generator Riched20 10.0.17134}\viewkind4\uc1 \pard\f0\fs20\lang1033 SS_\f1\fs24 100\par } " This is the RTF value and I am wondering what the best way is to strip this formatting and get just the value.
Perhaps there is another way to go about this that I am not thinking of as well. Any help would be appreciated.
So the ID column of DOORS is actually a composite- DOORS builds it out of the Module level attribute 'Prefix' and the Object level attribute 'Absolute Number'.
If you wish to grab this value in the future, I would do the following (using your variables)
string ostr = ( module ( o ) )."Prefix" o."Absolute Number" ""
This is opposed to the following, which (despite seeming to be a valid attribute in the insert column dialog) WILL NOT WORK.
string ostr = o."Object Identifier" ""
Hope this helps!
Comment response: You should not need the module name for the code to work. I tested the following successfully on DOORS 9.6.1.10:
Object o = current
string ostr = ( module ( o ) )."Prefix" o."Absolute Number" ""
print ostr
Another solution is to use the identifier function, which takes an Object as input parameter, and returns the identifier as a plain (not RTF) string:
Declaration
string identifier(Object o)
Operation
Returns the identifier, which is a combination of absolute number and module prefix, of object o as a string.
The optimal solution somewhat depends on your underlying requirement for retrieving the object ID.

NSLocalizedString sometimes returns the string other times shows the key

Using the same key sometimes returns the value othertimes it returns the key.
Code:
NSLog(#"%# : %#", NSLocalizedString(#"TESTSTRING", nil), NSLocalizedString(#"TESTSTRING", nil));
Output in console:
\^PTESTSTRING : Test String
Here are the key and value from the Localizable.strings file.
"TESTSTRING" = "Test String";
I see no differences between both localisation macros but one gets the value the other not. Has anybody ever come across this?
What does the \^P before the key signify in the console output?
Thanks,
NSC

How do I check if string is a file contents or path?

I have a method that receives a string. It can accept a file path or contents of the file.
When I pass path, it reads it and processes it, all good.
When I pass contents, it crashes with string contains null byte when it check if a file path exists
Here is the part of the code that checks if passed param is a file:
contents = File.exists?(param) ? File.read(param) : param
My question is: Is there another way to check if the param is a binary content vs path without File.exists?
You might check if the string contains null bytes upfront:
contents =
case input
when /\u0000/ then param
when File.exists?(param) then File.read(param)
else param
end
Or, even easier, fall back to param unless File#read succeeded:
contents = File.read(param) rescue param

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.

iOS - APNS how to use loc_key in payload

I have a message string i want to localize before i let APNS send a message to devices. I wish i could see the json payload itself to make me understand the structure instead of the device parsing it out for me. but anyway, how do i use the loc_key and loc_args argument ? from the docs i found:
loc-key
string
A key to an alert-message string in a Localizable.strings file for the current localization (which is set by the user’s language preference). The key string can be formatted with %# and %n$# specifiers to take the variables specified in the loc-args array. See Localized Formatted Strings for more information.
loc-args array of strings Variable string values to appear in place of
the format specifiers in loc-key. See Localized Formatted Strings for
more information.
and it was offical doc is here
I need a concrete example of how to localize a string and what is loc_args ?
I am just guessing here, but if i have a string localized like this:
mystring="cool localized message"
then the loc_key will be "mystring", is that right ?
loc-key is a string that needs to match a key in your Localizable.strings file inside the app bundle. For example if the loc-key is "my-push.hello-world" and you have the following line in your Localizable.strings file:
"my-push.hello-world" = "Hello, World!";
the push message displayed to the user would read "Hello, World".
log-args are strings that will be replaced in the message, like your have it in [NSString stringWithFormat:#"blabla %#"];
Apples example is this:
Push Notification Payload:
"alert" : {
"loc-key" : "GAME_PLAY_REQUEST_FORMAT",
"loc-args" : [ "Jenna", "Frank"]
}
Localizable.strings content:
"GAME_PLAY_REQUEST_FORMAT" = "%# and %# have invited you to play Monopoly";
if loc key is there, loc_key refers to a key in your localizable file...
e.g. if loc_key=XYZ, then there must be an entry "XYZ"="translated XYZ"; in the apps' localizable file.
loc_args is an array of strings that is to be inserted into the translated string as if you had used stringWithFormat:
e.g loc_args=["DOMI", "ME"] inserted into a translated XYZ like %# is %# results in "DOMI is ME"

Resources