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"
Related
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
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"
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 can I set ios application supported languages?
e.g I use NSDate to get current day. If the device language is other than my supported languages NSDateFormatter returns "day" in device's language but I want to get in English if I don't support that language.
I know there is a way to get day in specific language using NSLocal but I don't want to do that way because I need to convert other strings as well.
The Apple documentation covers this pretty clearly. I know all you need is the word "day", but the following will help you include any word for any language if you do as follows:
1) You need to place all of the words (Strings) in your application into a single .swift file. Each word should be returned in a function that converts this string into the localized string per the device's NSLocal set in the device settings:
struct Localization {
static let all: String = {
return getLocalized("All")
}()
static let allMedia: String = {
return getLocalized("All Media")
}()
static let back: String = {
return getLocalized("Back")
}()
// ...and do this for each string
}
2) This file should also contain a static function that will convert the string:
static func getLocalized(_ string: String) -> String {
return NSLocalizedString(string, comment: "")
}
Here, the NSLocalizedString( method will do all of the heavy lifting for you. If will look into the .XLIFF file (we will get to that) in your project and grab the correct string per the device NSLocale. This method also includes a "comment" to tell the language translator what to do with the "string" parameter you passed along with it.
3) Reviewing all of the strings that you placed in your .swift file, you need to include each of those into an .XLIFF file. This is the file that a language expert will need to go over and include the proper translated word per string in the .XLIFF. As I stated before, this is the file that once included inside your project, the NSLocalizedString( method will search this file and grab the correct translated string for you.
And that's it!
So, I'm localizing an app from japanese to english.
In japanese, there is no distinction between (say) "Mr." and "Ms."(/"Mrs."), so I need to do something like this:
/* Salutation format for user (male) */
"%#様" = "Mr. %#";
/* Salutation format for user (female) */
"%#様" = "Ms. %#";
As you can see, the Japanese localization uses the same string in both cases. I was under the impression that, when the strings file contains more than one entry with the same 'source' (i.e., left side) string, the 'comment' was used to determine which one was employed. It turns out I was wrong, and the comment parameter is just an aid for human translators, totally ignored by NSLocalizedString().
So if I run the app, both of the code paths below produce the same string, regardless of the user's gender:
NSString* userName;
BOOL userIsMale;
/* userName and userIsMale are set... */
NSString* format;
if(userIsMale){
// MALE
format = NSLocalizedString(#"%#様",
#"Salutation format for user (male)");
}
else{
// FEMALE
format = NSLocalizedString(#"%#様",
#"Salutation format for user (female)");
}
NSString* salutation = [NSString stringWithFormat:format, userName];
So, how should I deal with a case like this?
Well, actually “left side” of the NSLocalizedString is a key. So you could do something like:
NSLocalizedString(#"SalutForUserMale", #"Salutation format for user (male)");
NSLocalizedString(#"SalutForUserFemale", #"Salutation format for user (female)");
Then in your base *.strings file (Japanese I presume) you would have:
"SalutForUserMale" = "%#様";
"SalutForUserFemale" = "%#様";
And in your English *.strings file you would have:
"SalutForUserMale" = "Mr. %#";
"SalutForUserFemale" = "Ms. %#";
The Localizable.strings files are nothing more than key value lists. You are using the Japanese phrases as keys which is unusual but I guess it works. I assume this is because the original app was developed in Japanese(?). I usually use English phrases keys, but whatever.
The point is that if you want two different phrases in even just one translation you have to have two different keys in all your translations. If there is something in your base language that is not distinguished but in the translations it is, then you can just "split" an existing key in two new ones. Just change it slightly or add a number, see below:
/* english .strings file */
"hello_world_key" = "Hello World";
"Yes1" = "Yes"; // same in english but different values in german
"Yes2" = "Yes";
/* german .strings file */
"hello_world_key" = "Hallo Welt";
"Yes1" = "Ja";
"Yes2" = "Jawohl";