How to localize specific phrasing on iPhone? - iphone

I have a few phrases on iPhone that translate in diverse logical blocks in some languages.
For example, in English it will be:
You have 5 camels in the Paris store
where in French it will be
Vous avez 5 chameaux dans le magasin de Paris
I don't quite get how to solve this, given the number of elements, the type of element (camel in this exemple) and the place are variable. I chose camels in this example on purpose, because you can't simply add a "S" at the end to mark multiplicity as in English or with some other words. If it was bottles we were talking about:
Vous avez 5 bouteilles dans le magasin de Paris
Does anyone have an idea? I don't really think I should implement a grammatical system to decide whether to add X, S or whatever else is the mark of multiplicity in the language used...
I should make clear that the NSLocalizedString currently are used inside a NSStringWithFormat of the form #"%# %d %# %# %#", with the second and the last %# built on the fly from an appropriate language database, and the first as well as third %# gotten from the localization strings file. Also, to elaborate on #rckoenes's answer, what he suggested is already implemented, but as I explained it's not the problem I have an issue with :D

What I would do is create to translations with string format in the Localizable.strings file.
The in code do something like:
NSString *camelStringFormat = nil;
if (numberOFCamels > 1) {
camelStringFormat = NSLocalizedString(#"OnCamel", #"Translation for one camel");
} else {
camelStringFormat = NSLocalizedString(#"MultipleCamels", #"Translation for multiple camels");
}
NSString *camelString = [NSString stringWithFormat:camelStringFormat, numberOFCamels, city];
Where Localizable.strings for englis would look like:
"OnCamel" = "You have %d camel in the %# store"
"MultipleCamels" = "You have %d camels in the %# store"
Where Localizable.strings for france would look like:
"OnCamel" = "Vous avez %d chameau dans le magasin de %#"
"MultipleCamels" = "Vous avez %d chameaux dans le magasin de %#"

Actually, after careful consideration, it is simpler to iconize this issue, by which I mean, use graphical elements instead of text. This way avoids the issue of multiple grammatical rules in languages. If anyone knows of an iPhone API that deals with grammatical localization though, I'm still intellectually interested. Among other things, because though I never would have expected so, the iPhone 4S advertisements showed that visually-impaired people use the iPhone and icons are not very well suited to them :D
It also allows me to transform a screen that was getting cluttered and streamline the experience.

Related

Set newline and paragraph in string with objective C

When i parse the jason url i got string with character \n \r ... Now i want break the line where with "\n" and put the paragraph with "\r".
i got string like this.
description = "Black Chasm Cavern was designated a National Natural Landmark by the National Park Service in 1976 after being recommended by local members of the National Speleological Society, and as such is considered a \"nationally significant natural area.\U201d\n\nVisitors to the cave are enchanted by the beauty of a wide variety of formations including stalactites, stalagmites, flowstones and the vast arrays of rare helictite crystals, for which Black Chasm is justly famous.\n\nThe 45-minute walk tour follows a series of platforms, stairs and walkways designed to give the best views of the cave without compromising the naturally pristine environment. Currently, the tour culminates in a visit to the Landmark Room, the location of the greatest collections of sparkling helictite crystals.\n\nAbove ground, kids love our gemstone mining at our mining flumes right outside the Visitors Center. Everyone is guaranteed to find some real gemstones; the perfect start to a rock collection! Try our new incredibly popular geode cracking too!";
and i want to display this string in label as per format.
how can i do this job if any one have any idea please tell me ...
Thanks.
I would suggest iterating through your string, separating it into an array of lines and paragraphs. Like the following.
NSArray *lines = [string componentsSeparatedByString:#"\n"];
for(NSString *line in lines)
{
NSArray *lineElements = [line componentsSeparatedByString:#"\r"];
// do something with lineElements
}

How to Display International Accents with Quartz/Core Graphics on iPhone

I've localized an app for the iPhone. No surprise, the localization includes some accents:
"Touch cards to select. Then touch
'Bid'." = "Touchez les cartes pour les
sélectionner, puis touchez 'Miser'.";
These work fine in high-level stuff, like when I put the text into a table, but when I try to write them to a UIView by hand, the accents get mangled:
I'm using kCGEncodingMacRoman and UTF8, both of which should support accents, I think, but I'm clearly missing something:
CGContextSelectFont(ctx,fontName,thisWriting.fontSize,kCGEncodingMacRoman);
CGContextShowTextAtPoint(ctx,
thisWriting.center.x - floor(thisTextSize.width/2),
yThusFar,
[thisText UTF8String],
[thisText length]);
The font is some variant of ArialMT. thisText is just an NSString.
Quartz provides a limited, low-level interface for drawing text. For information on text-drawing functions, see CGContext Reference. For full Unicode and text-layout support, use the services provided by Core Text or ATSUI).
To expand on what sorin said: Quartz/Core Graphics do not support Unicode text, which includes the accents you need for foreign languages. There have traditionally been a number of alternative ways to resolve this, but currently the best answer is to use Core Text, which can write directly to a graphical context, as I was doing here.
The main element in Core Text is the NSAttributedString or NSMutableAttributed String class.
Here's similar code to what I had for Core Text:
CTLineRef thisLine = CTLineCreateWithAttributedString((CFAttributedStringRef)thisAText);
CGContextSetTextPosition(ctx, self.center.x - floor(thisTextSize.width/2), yThusFar);
CTLineDraw(thisLine, ctx);
The font is already taken care of, because it's part of that NSAttributedString (or CFAttributedStringRef, which is a toll-free bridged equivalent).
Here's the result:

Problems with escaped characters in JSON string

I have a JSON-string where I know where the problem is, I just can't figure out what to do. I have looked up the "forbidden characters" in a JSON-string but it just doesn't work.
When you run the show-method for FBStreamDialog for iPhone a view comes up with how it's going to look like when it's finally posted on the wall.
This happends when the "description"-property in my JSON-string is hard coded like #"Testing". But as soon as I add the text fetched from a data source which looks like this, it doesn't work:
"description":"
LIVE: Uk's No:1 Reggae Singer Bitty Mclean + Joey Fever, Sthlms No:1 Reggae Voice.
DJs
Deejay Flash & Micke Goulos + Mc Fabulous G.
The Vinyl Bar
Up...
"
Note: I only show the "description"-property of the JSON-string, because there is where the problem is.
So what I tried to do was, as I've explained before, to add the string "Testing" in the "description"-property. This worked. But I wanted to have the data source property of "description", of course. So I tried to replace all the characters that isn't a letter with this code:
shortString = [shortString stringByReplacingOccurrencesOfString:#"&" withString:#"och"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"+" withString:#"plus"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"," withString:#"komma"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"'" withString:#"apostrof"];
shortString = [shortString stringByReplacingOccurrencesOfString:#":" withString:#"colon"];
The output of that is:
"description":"LIVEcolon Ukapostrofs Nocolon1 Reggae Singer Bitty Mclean plus Joey Feverkomma Sthlms Nocolon1 Reggae Voice.
DJs
Deejay Flash och Micke Goulos plus Mc Fabulous G.
The Vinyl Bar
Up...
Which looks like a approvable JSON string?
But apparently not, because the facebook view never shows how it's going to look if I use the data source "description"-property. It just shows the text box "What's on your mind".
This is driving me crazy.
Finally!
I understand why you didn't answer this question. How could you know that facebook connect doesn't allow \n in their StreamDialog's. Not for iPhone anyway.
So the solution was to replace \n with a whitespace or whatever you want.

Localizing iPad applications

I am creating an iPad application, and I have a Localizable.strings file, which is in English and Dutch:
English.lproj/Localizable.strings
"failed to copy file" = "Failed to copy file";
"failed to copy defaultFeeds.plist" = "Failed to copy defaultFeeds.plist form the application's bundle to the device's documents directory for this app. Please make sure the device has space available on the flash memory. Please push the home button, connect your device to iTunes and check how much space there is available. Remove things you don't need and try again. If the problem presists, please contact us. You can find our support page in the App Store in iTunes.";
Dutch.lproj/Localizable.strings
"failed to copy file" = "Bestand kopieëren mislukt";
"failed to copy defaultFeeds.plist" = "Het kopieëren van defaultFeeds.plist van de applicatiebundel naar de documenten map van het apperaat is mislukt. Ga alstublieft na of het apparaat ruimte heeft op het flash geheugen. Druk op de Home-knop, verbind het apparaat met iTunes en controleer hoeveel ruimte er beschikbaar is. Verwijder dingen die je niet nodig hebt en probeer het opnieuw. Als de problemen voortzetten, kunt u contact met ons opnemen. U kunt onze support-page vinden in de App Store in iTunes.";
When I comment out error checking, so I get the error for sure, I get the English version, although the iPhone Simulator's language is set to Dutch:
// Copy defaultFeeds if run for the first time
//if(![[NSFileManager defaultManager] fileExistsAtPath:FEEDS_PLIST_IN_CURRENT_APP_DIRECTORY_PATH]) {
// if(!) is for handling errors
//if(![[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:#"defaultFeeds" ofType:#"plist"] toPath:FEEDS_PLIST_IN_CURRENT_APP_DIRECTORY_PATH error:nil]) {
UIAlertView *copyFailedAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"failed to copy file", #"failed to copy file") message:NSLocalizedString(#"failed to copy defaultFeeds.plist", #"failed to copy defaultFeeds.plist") delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[copyFailedAlertView show];
[copyFailedAlertView release];
//}
//}
Also, the debugger says:
There is no HW layout for this input mode, defaulting to US
Can anyone help me? Thanks.
Did you create the Localizable.strings file through the Xcode4 interface?
The 'old style' naming (English.lproj, Dutch.lproj) has been replaced by the standard format (en.lproj, nl.lproj). Select the 'Localizable.strings' file and on the right hand side helper UI:
See if that changes anything for you.

iPhone en_* sublanguage localization

I want to localize strings in my iphone app for en_GB and other 'en' sub-languages, but XCode and the iphone refuse to let this happen. I have created a localization of "Localizable.strings" for en_GB and en_US (I tried both hyphens and underscores) for testing purposes, but they just aren't recognized. The only language code that works is simply "en" (displayed as "English" in XCode).
I can't believe this isn't possible, so what am I doing wrong? I'm also hoping to get the typical 'cascading' behaviour where if a string isn't found in the sub-language e.g. "en_GB" then it should be taken from "en" if possible. Help?
When you choose 'English' from the list of languages on the iPhone preferences, that actually means the 'en_US' language.
So until apple update their software with additional sublanguages like "English (British)" etc. we are left with going by the locale region setting, and loading strings manually from another string table.
However, the language and regional locale are separated for a reason: a Spanish user in the UK may want dates/times formatted according to the local customs, but program strings in their native tongue. It would be incorrect to detect the regional locale (UK) and therefore display UK strings.
So basically there is no way to do this currently.
What you're doing should work according to the docs. But it appears that the iPhoneOS implementation is at odds with the documentation. According to Radar 6158876, there's no support for en_GB language, only locale (date formats and the like).
I found the same problem.
BTW, if you look at the iPhone Settings -> General -> International menu, it makes the distinction between language and region quite clear:
Languages:
-English
Region Format:
-United States
-United Kingdom
The localization framework only appears to pay attention to the language, not the region.
I'm tempted to raise an enhancement request for this with Apple, as IMO it is reasonable that a user might want to use British English (for the text) whilst being in the United States (where, say, phone numbers should be in US format).
This can actually be done - check my solution here - iPhone App Localization - English problems?
Create a separate string resource, say UKLocalization.strings, and create localizations for each of your supported languages. For all localizations other than en this file is empty. For en, it contains only the strings that have unique en_GB spelling.
Next, you create a replacement for NSLocalizationString that will first check the UKLocalization table before falling back to the standard localization table.
e.g.:
static NSString* _locTable = nil;
void RTLocalizationInit()
{
_locTable = nil;
NSString* country = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
if ([country isEqual:#"GB"])
{
_locTable = #"UKLocalization";
}
}
NSString* RTLocalizedString(NSString* key, NSString* ignored)
{
NSString* value = nil;
value = [[NSBundle mainBundle] localizedStringForKey:key value:nil table: _locTable];
if (value == key)
{
value = NSLocalizedString(key, #"");
}
return value;
}
I’m not sure in which version of iOS it was introduced, but iOS 7 definitely has a ‘British English’ language preference that will pick up resources from the en_GB.lproj directory. The various hacks floating around the web shouldn’t be necessary unless you’re after a more specialised* dialect.
*see what I did there ;)