Formatted NSLocalizedString with variable value - ios

I'm new to Objective-C. Here is my question:
In my two string files are the following two entries:
(German string file)
/* Class = "IBUILabel"; text = "Import to DoS"; ObjectID = "GfF-rD-aDa"; */
"GfF-rD-aDa.text" = "Zu DT %lu importieren";
(English string file)
/* Class = "IBUILabel"; text = "Import to DoS"; ObjectID = "GfF-rD-aDa"; */
"GfF-rD-aDa.text" = "Import to DoS %lu";
My code looks like:
self.importLabel.text = [NSString localizedStringWithFormat:NSLocalizedString(#"GfF-rD-aDa.text", nil), projectday];
According to Apples Documentation NSLocalizedString needs a key and a comment. That's why I putted #"GfF-rD-aDa.text" into the first parameter because it's the same key like in my strings file.
So I want it to generate strings like:
"Zu DT 2 importieren"
and
"Import to DoS 2"
but it doesn't work. The output text is:
"GfF-rD-aDa.text"
I'm not allowed to change the key in the strings table because we use a script to generate all these entries based on the object id.
Regards

Try using this specify your strings file in NSLocalizedStringFromTable macro.
self.importLabel.text = [NSString stringWithFormat:NSLocalizedStringFromTable(#"GfF-rD-aDa.text", #"yourStringsFile", #"comment"), projectday];

Try this:
self.importLabel.text = [NSString stringWithFormat:NSLocalizedString(#"GfF-rD-aDa.text", nil), projectday];

Related

Localizable.strings key-value pair

I want to localize "1 of 9", and 1 and 9 are int parameters, my code is as below
context = [NSString stringWithFormat:NSLocalizedString(#"%d of %d",
"This text will be used to show page number on the screen"),
currentPageIndex + 1, pageCount];
And the generated Localizable.strings show like that
/* This text will be used to show page number on the screen */
"%d of %d" = "%1$d of %2$d";
I think the thing on the left of "=" is key and the the thing on the right of "=" is value, but I want the key looks like "show_page_number" not included the format "%d", how can I do? I try to replace "%d of %d" with "show_page_number", but it does not work. Any advice?
NSLocalizedString() replaced key with value at runtime .So you can use anystring as key & it will be replaced as "%1$d of %2$d" at runtime.
Add string in Localizable file :
"show_page_number" = "%1$d of %2$d";
& in code use that key
context = [NSString stringWithFormat:NSLocalizedString(#"show_page_number", "This text will be used to show page number on the screen"), currentPageIndex + 1, pageCount];
Add localizable.string file in xcode project.
If you want full control over the key and its initial value you need to use NSLocalizedStringWithDefaultValue instead of NSLocalizedString (which uses the key as the initial value).
Change your code to:
NSString *format = NSLocalizedStringWithDefaultValue(#"show_page_number", #"Localizable", NSBundle.mainBundle, #"%1$d of %2$d", #"This text will be used to show page number on the screen")
context = [NSString stringWithFormat:format, currentPageIndex + 1, pageCount];
When you run genstrings, you will get:
/* This text will be used to show page number on the screen */
"show_page_number" = "%1$d of %2$d";

What is wrong with this NSLocalizableString code?

I have this code on Viewcontroller.m, on Xcode:
NSString *language = NSLocalizedString(#"es", #"language");
NSString *connector = NSLocalizedString(#"de", #"connector to link words");
And this one on the "Localizable.strings (English)":
"language" = "en";
"connector to link words" = "of";
The problem is that with every language I change on the iOs Simulator, I always get the first value, the value of the Viewcontroller.m, instead of get the strings values.
Does anyone know what is wrong?? Thank you so much!
UPDATE:
I have this:
NSString *language = NSLocalizedString(#"es", #"language");
NSString *connector = NSLocalizedString(#"de", #"connector to link words");
But it still doesn't work!! Why????
It only shows the key values!! In the strings I have:
"es" = "en";
"de" = "of";
on the english file, and on the spanish file:
"es" = "es";
"de" = "de";
SOLUTION:
I think I have already done everything right, so the problem must to be in the iOs simulator. If anyone can take advantage of that, my solution has been edit the scheme clicking in the image of the project in the superior task bar, and in the tab "Options" (on the Run part) set "Spanish" as my language by default.
Thanks everybody anyway.
The syntax of NSLocalizedString goes like the below.
NSString * NSLocalizedString(
NSString *key,
NSString *comment
)
The key should be used in your .strings file. The value of the key will be different for different languages .
So when you run the key will be replaced by the value provided in the language .strings file you set.
Look at this tutorial for more explanation.
syntax is NSLocalizedString(key, comment)
And this one on the "Localizable.strings (English)":
"language" = "en";
"connector to link words" = "of";
so "language" is the key and "en" is the value
so
NSString *language = NSLocalizedString(#"language", #"");
NSString *connector = NSLocalizedString(#"connector to link words",#"");

How to perform Localization using sqlite stored data-iOS App?

In my app i am doing localization to support mutiple languages. For that i am creating .string file and using the following method:
-(NSString*) languageSelectedStringForKey:(NSString*)key;
I am using the Key_value concept for this. its working fine. But My question is:
Let say i have a sqlite database which has all the different languages string which needs to be localized.
For eg: for spanish
"Username" = "nombre de usuario";
"Password" = "contraseƱa";
"Submit" = "presentar";
for french
"Username" = "nom d'utilisateur";
"Password" = "mot de passe";
"Submit" = "soumettre";
all this is insert in a sqlite database.
How can we perform localization with the app that way?
Since you seem to have all strings traduction within one db, you need to have an intermediate data structure that will act as a bridge between the db and your method to localize the string.
You could make use of a dictionary for e.g to store the key string identifier and as a value an array that contain all the languages traduction. You will need to feed this data structure once then.
Note that the recommended way is to use .strings files for localisation so you need to provide us good raison why you can't take this approach.
If those are titles/texts for UIButtons and UILabels use the following syntax in your viewController:
NSString *buttonString = NSLocalizedStringFromTable(#"viewController-title-button-username", #"Localizable", nil);
self.username_label.text = buttonString;
And in your Localizable.strings file you do(you must add localization to your project and set this file to be localized to spanish):
"viewController-title-button-username" = "nombre de usuario";
"tabbar-item1" = "item1 in spanish";
"tabbar-item2" = "item2 in spanish";
"tabbar-item3" = "item3 in spanish";
And the same for the Localizable.strings you have set to be the french:
"viewController-title-button-username" = "nom d'utilisateur";
"tabbar-item1" = "item1 in french";
"tabbar-item2" = "item2 in french";
"tabbar-item3" = "item3 in french";
For your tabbar name, put this in your viewDidLoad for each viewController you have. Just change 1, 2, 3 etc.:
NSString *name = NSLocalizedStringFromTable(#"tabbar-item1", #"Main", nil);
self.title = name;
On the other hand if you are getting either an .xml or retrieving using REST you can do the following:
//Get the name of what you want eg. spanish-data or french-data
NSString *name = NSLocalizedStringFromTable(#"xml-locations-string", #"Main", nil);
//What to add at the end
NSString *append = #".xml";
//Merge strings to what you want to retrieve: spanish-data.xml
NSMutableString *file = [NSMutableString stringWithFormat:#"%#%#", name, append];
//From which URL
NSString *urlString = #"http://www.myDomain.com/ProjectName/";
//Create full URL http://www.myDomain.com/ProjectName/spanish-data.xml
NSString *fileUrl = [NSString stringWithFormat:#"%#%#", urlString, file];
NSURL *url = [NSURL URLWithString:fileUrl];
The data you get back will now be the localized version. And you can use that to set table data or whatever you need.
This is the super simple and therefor not optimized way of doing it. Works for REST as well, you just need to reconstruct to fit your project.

Why do some dictionary keys have quotes and others don't when printing from debugger?

So I am currently sending back from my server a JSON object. If I do a print of the responseObject in XCode, I get the following:
Printing description of responseObject:
{
order = 3;
"picture_bucket_name" = test;
"picture_creation_date" = "2013-01-06T21:49:54.546Z";
"picture_identifier" = 61;
"picture_url_with_bucket" = "test/pictures/sop/steps/test_default_320_320.png";
"sub_order" = 0;
}
Why is the Order key not in " "? This is the only key with a Number that I have to convert from NSString to NSNumber using a NumberFormatter. Why is that?
Normally a single word without any spaces and other special characters are shown without any quotes in console for the dictionary key. For eg:- the key order and the value test in above example. That is helpful for maintaining the readability. Wrapping these other words in quotes ensure the readability.

Search NSString in line from file

Is it possible to make a function that searchs a string for an exact substring so that it will only 'return true' if the exact string if found, not as part of a larger word?
NSString* search = #"tele";
NSString* stringOne = #"telephone";
NSString* stringTwo = #"tele phone";
NSString* stringThree = #"phone tele";
What I mean is: Is it possible to search for a string in a way that the NSString 'search' would be found in strings Two and Three, but not One?
Try using the following function in the NSString class:
- (NSRange)rangeOfString:(NSString *)aString
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsstring_Class/Reference/NSString.html
Simplest approach is to append blanks (or whatever your separators are) to front and rear of both strings, then do the search.

Resources