Localization with %# - localization

I am trying to localize the text "System Name:" in the code below:
NSString *systemName = [NSString stringWithFormat:#"System Name: %#", [[UIDevice
currentDevice] systemName];
I do this by changing the code to this:
NSString *systemName = NSLocalizedString(#"SystemNameKey", #"System Name Info");
In my Localizable.strings file, I add the following code:
"SystemNameKey" = "System Name: %#", [[UIDevice currentDevice] systemName];
Of course, this will not work because UIKit is not imported into the Localizable.stings, and not surprisingly, when I add the import code, it does not work. I am sure there is an alternate way of doing this that I'm just not thinking of at the moment. Any ideas? I feel like I'm missing something really obvious.

You should try doing it this way:
NSString *systemNameLocalized = NSLocalizedString(#"SystemNameKey", #"System Name Info");
NSString *systemName = [NSString stringWithFormat:systemNameLocalized, [[UIDevice currentDevice] systemName]];
and in your Localizable.string file:
"SystemNameKey" = "System Name: %#";

Related

iOS9 AppleLanguages different from older iOS

How I get now the actual system language? It seems that they put regional suffix after last dash. So before cs is now cs-DE if the language is Czech and regional setting is German. But there are some languages which don't have the suffix like GB language is en-GB but regional setting is German.
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* language = [defs objectForKey:#"AppleLanguages"];
NSString* preferredLang = [language objectAtIndex:0];
NSLog(#"localeIdentifier: %#", preferredLang);
Use the componentsFromLocaleIdentifier method from NSLocale class
Here is the documentation
You can do like this:
NSString* localeID = [NSLocale currentLocale].localeIdentifier;
NSDictionary* components = [NSLocale componentsFromLocaleIdentifier:localeID];
NSString* languageID = components[NSLocaleLanguageCode];
EDIT
Getting the language this way will create some issues if the language the app is currently translated in is not the device's language. Indeed,
components[NSLocaleLanguageCode] will return the device's language.
To get the app's current language, you should use [[NSBundle mainBundle] preferredLocalizations].firstObject.
To get the device's region, you can still use components[NSLocaleCountryCode]
I just run into this problem recently. According to Apple's documentation, you will get the locale id with region designator which for like [language designator]-[region designator] on iOS 9.
I found a solution if you just wanna get the locale id, you could use
[[NSBundle mainBundle] preferredLocalizations].
One more solution, If any of you like,
NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
if ([[currentLanguage componentsSeparatedByString:#"-"] count] == 2)
currentLanguage = [[currentLanguage componentsSeparatedByString:#"-"] objectAtIndex:0];
// Only for chinese Language.
else if ([[currentLanguage componentsSeparatedByString:#"-"] count] == 3)
currentLanguage = [NSString stringWithFormat:#"%#-%#", [[currentLanguage componentsSeparatedByString:#"-"] objectAtIndex:0],
[[currentLanguage componentsSeparatedByString:#"-"] objectAtIndex:1]
];
"currentLanguage" Will give you your current langauge so you can use it for localise or any further use.

How to get UDID of iOS Device Programmatically in iOS 7

i am using the below code to display the UDID of the device.
But its displaying the null value
i.e. 2014-05-12 11:56:06.896 LoginScreen[195:60b] deviceUDID: (null)
NSUUID *deviceId;
deviceId = [UIDevice currentDevice].identifierForVendor;
NSLog(#"deviceUDID: %#",deviceID);
Sorry to all of you. I made a silly mistake
Here NSUUID instance is deviceId and i am printing deviceID in NSLog :)
Now it is working for me. Thanks to everyone
In order to get UUID of the device you can use the following line of code
[[[UIDevice currentDevice] identifierForVendor] UUIDString];
But you have to check whether the app is running on simulator or on device.
hope this helps you.
Apple has hidden the UDID from all public APIs, starting with iOS 7. Any UDID that begins with FFFF is a fake ID.
Objective-C :
NSString *strUUIDValue = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSLog(#"strUUIDValue : %#", strUUIDValue);
Swift 2 :
let UUIDValue = UIDevice.currentDevice().identifierForVendor!.UUIDString
print("UUID: \(UUIDValue)")
NSString *string = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
string = [string stringByReplacingOccurrencesOfString:#"-" withString:#""];
string = [NSString stringWithFormat:#"%#%#",#"FFFFFFFF",string];
Try this
NSUUID *deviceId;
#if TARGET_IPHONE_SIMULATOR
deviceId = [NSUUID initWithUUIDString:#"UUID-STRING-VALUE"];
#else
deviceId = [UIDevice currentDevice].identifierForVendor;
#endif
OR
NSString *uuid = nil;
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
uuid = NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID));
[uuid autorelease];
CFRelease(theUUID);
}

Adding a percentage % sign to an NSString [duplicate]

I want to have a percentage sign in my string after a digit. Something like this: 75%.
How can I have this done? I tried:
[NSString stringWithFormat:#"%d\%", someDigit];
But it didn't work for me.
The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.
The escape code for a percent sign is "%%", so your code would look like this
[NSString stringWithFormat:#"%d%%", someDigit];
Also, all the other format specifiers can be found at Conceptual Strings Articles
If that helps in some cases, it is possible to use the unicode character:
NSLog(#"Test percentage \uFF05");
The accepted answer doesn't work for UILocalNotification. For some reason, %%%% (4 percent signs) or the unicode character '\uFF05' only work for this.
So to recap, when formatting your string you may use %%. However, if your string is part of a UILocalNotification, use %%%% or \uFF05.
seems if %% followed with a %#, the NSString will go to some strange codes
try this and this worked for me
NSString *str = [NSString stringWithFormat:#"%#%#%#", #"%%",
[textfield text], #"%%"];
uese following code.
NSString *searchText = #"Bhupi"
NSString *formatedSearchText = [NSString stringWithFormat:#"%%%#%%",searchText];
will output: %Bhupi%
iOS 9.2.1, Xcode 7.2.1, ARC enabled
You can always append the '%' by itself without any other format specifiers in the string you are appending, like so...
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [stringTest stringByAppendingString:#"%"];
NSLog(#"%#", stringTest);
For iOS7.0+
To expand the answer to other characters that might cause you conflict you may choose to use:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
Written out step by step it looks like this:
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [[stringTest stringByAppendingString:#"%"]
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];
NSLog(#"percent value of test: %#", stringTest);
Or short hand:
NSLog(#"percent value of test: %#", [[[[NSString stringWithFormat:#"%d", test]
stringByAppendingString:#"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);
Thanks to all the original contributors. Hope this helps. Cheers!

Trouble with ID in NSString

I am trying to create something interesting in my application. So, I created an UILabel and I want to output new value.
So, my code.
NSString *test = #"13";
self.UserAge.text = #"Your age is %#", test;
But it doesn't work.
In Console-Command Mode I can do it with NSLog();
My result is "Your age is %#". But I need to output "Your age is 13". What do I should do with name?
Sorry, if my question is easy for you. I am beginner. :)
Thank you everyone who will answer on my question.
You want:
NSString *test = #"13";
self.UserAge.text = [NSString stringWithFormat:#"Your age is %#", test];
Your version is equivalent to:
NSString *test = #"13";
self.UserAge.text = test;
And I would suggest you use the correct data type, which for an age is an integer:
NSUInteger age = 13;
self.UserAge.text = [NSString stringWithFormat:#"Your age is %ld", age];
// this might be %d, depending on platform ^^^
You need to do the following:
NSString *test = #"13";
self.UserAge.text = [NSString stringWithFormat:#"Your age is %#", test];

ios localizable strings are not working

I have made an ios app that is localized to two languages (english and danish, english being default).
I have made a Localizable.strings with two subfiles, en and da.
Everything should be made correctly, and i see the english texts load fine from the strings file. But the danish ones do not.
I have tried to check the preferred language via below code and it is danish.
[[NSLocale preferredLanguages] objectAtIndex:0];
I have tried clean + delete and rebuild with no luck.
I know that the Localizable.strings file is working since it is getting the english texts. and i know that it is seeing the danish localization via the above line of code.
What am i missing?
Just to add a couple of examples:
from the EN:
"YesButton" = "Done";
"NoButton" = "Not yet!";
"NextButton" = "Next";
"BackButton" = "Back";
"EditButton" = "Edit";
"DoneButton" = "Done";
and the DANISH:
"YesButton" = "Færdig";
"NoButton" = "Ikke endnu!";
"NextButton" = "Næste";
"BackButton" = "Tilbage";
"EditButton" = "Redigér";
"DoneButton" = "Færdig";
and the code for getting the text would be:
[yesButton setTitle:NSLocalizedString(#"YesButton", nil) forState:UIControlStateNormal];
which is returning "Done" even when preferredLang is da (danish)!
Hope somebody has an idea? :)
Thanks in advance.
EDIT:::
Something was wrong with the actual DANISH-DENMARK localization. i dont know if apple updated it or what they did but it went from being called "Danish-Denmark" to just "Danish".
After making a whole new Danish localization and deleting the old one it worked! crazy stuff. keeps you scratching your head!
I had similar thing happened to me before. All I did to fix the problem was to change the string encoding from:
encoding:NSASCIIStringEncoding
to
encoding:NSUTF8StringEncoding
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
Now assume the problem is not string encoding. You can also work around it and do something like:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString yesButtonStr=#"";
NSString noButtonStr=#"";
NSString nextButtonStr=#"";
if([#"en" caseInsensitiveCompare:language] == NSOrderedSame )
{
yesButtonStr = #"Done";
noButtonStr= #"Not yet!";
nextButtonStr = #"Next";
//...
}
else if if([#"da" caseInsensitiveCompare:language] == NSOrderedSame )
{
yesButtonStr = #"Færdig";
noButtonStr = #"Ikke endnu!";
nextButtonStr = #"Næste";
//...
}
Then:
[yesButton setTitle:NSLocalizedString(yesButtonStr, nil) forState:UIControlStateNormal];
Try this,
NSString *translatedString = [self languageSelectedStringForKey:#"YesButton"]; // Give your key here
-(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"]; // give your language type in pathForResource, i.e. "en" for english, "da" for Danish
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:#"" table:nil];
return str;
}
Hope this helps, Thanks. Happy coding
There was something wrong with the actual danish localization.
When i started making the localization it was called "Danish-Denmark". After an update of ios and/or xcode, it is now only "Danish".
So had to make a whole new localization.
Thank you apple...... NOT!

Resources