Localization of a variable value on iOS - iphone

I have around 72 html files in my resource folder and I need to localize them. Now I have translated them in french. A file selection depends on user input so file name is created by a variable. Now the problem is how to localize the value of a variable.
For example, I have following 3 files in resource folder.
AAAAA0.html
BBBBB28.html
CCCCC33.html
I also have these files with in french in resource folder.
AAAAA0-French.html
BBBBB28-French.html
CCCCC33-French.html
Here is my code that is working fine without localization.
// ViewController.m File
appDelegate2=[[[UIApplication sharedApplication] delegate] retain];
NSString *getSign2=[appDelegate2.globalString stringByAppendingString:appDelegate2.globalindex];
NSString *filePath=[[NSBundle mainBundle] pathForResource:getSign2 ofType:#"html" ];
If user input causes the selection of AAAAA0.html then globalString will be "AAAAA",globalIndex will be "0" and getSign2 will Be "AAAAA0".
If user input causes the selection of BBBBB28.html then globalString will be "BBBBB",globalIndex will be "28" and getSign2 will Be "BBBBB28".
If user input causes the selection of CCCCC33.html then globalString will be "CCCCC",globalIndex will be "33" and getSign2 will Be "CCCCC33".
Now I already have a Localizable.string file and my .xib files are already localized(all labels,images and datepicker etc.).
I want to change the value of getSign2 from AAAAA0.html to AAAA0-French.html if user language is french. Similarly it should change to BBBB28-French.html and CCCC33-French.html for BBBB28.html and CCCC33.html respectively.
What should I add in localizable.string to do this? What change should I make in ViewController.m file?
I know I can use following
NSLocale *locale=[NSLocale currentLocale];
NSString *currentlocale =[locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]];
NSLog(#"Complete Locale: %#",currentlocale);
if (currentocale==#"French") {
NSString *getSignNew=[getSign2 stringByAppendingString:#"-French"];
NSString *filePath=[[NSBundle mainBundle] pathForResource:getSignNew ofType:#"html" ];
}
But this method check conditions in ViewController.m file while I have done all other localization related coding in localizable.string. Will it cause problem when I will submit my application? If this is not the correct solution then please tell me how to fix this problem.
Please reply as soon as possible.
Thanks.

Localized file should be placed in the structure like this:
Your.app/
English.lproj/
AAAAA0.html // English localized file
....
French.lproj/
AAAAA0.html // French localized file
...
zh_TW.lproj/
AAAAA0.html // Chinese localized file
...
and then -pathForResource:ofType: will automatically look up the correct file matching the current locale.

Related

Cannot load country-specific resource

I am loading text resources in different languages from text files using:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"txt"];
if(filePath) {
NSString *myText = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
}
Works very well with my language specific resources in English, German etc.
But I do also have some country-specific resources, like an English text for United Kingdom, an English text for USA, a German text for Austria etc.
I would expect these resources to be loaded if the phone is set to the specific country (Region Format), but they don't. The filepath will always be set to the file in the "pure" language folder.
So for example I have myfile.txt in the folders:
en.lproj
en-GB.lproj
en-US.lproj
but it always loads the one in en.lproj
Folders have been created using the localization function in Xcode and are all located in the same folder.
What is a bit odd is that these folders created by Xcode do seperate language and country by a hyphen, but according to the documentation it should be an underscore. I have tried to copy folders (with underscores) into the project manually, but it still doesn't work.
How do I manage to load the country-specific files?
Thanks.....
I created a "dutch-netherlands" translation of Localisations.string and set iPhone language to dutch, but NSLocalizedString always returned the English translation. The solution was to create a "dutch" translation (nl.lproj) instead of "dutch-netherlands" (nl-NL.lproj).
In the settings screen of iOS4 or iOS5, you can select Dutch language but there's no subselection for Dutch-Netherlands or Dutch-Belgium, even though these are available in XCode. It seems to me that XCode offers more translation options than that are actually supported by iOS.
I mean there's no way to make your app use "nl-NL" resources. I guess the same problem goes for German and Austria ("de-DE" and "de-AT").
Note: Your code sample is a complicated way of loading text resources. It's fine if you are loading non-string resources such as images or media files, but for simple strings it's much easier to just use one .strings file per language (containing multiple localized strings) and call NSLocalizedString to get the relevant language version of a given string.
Answer: The iOS setting under "Region Format" does not affect resource loading. Instead it controls how the system performs locale-dependent functionality (such as date/number formatting and parsing). The iOS setting that controls which language version of your localized resources gets loaded is the Language setting (General > International > Language). Currently, it includes only two English variants: "English" and "British English".
I have the same problem with nl-NL.lproj (Netherland) and ca-ES.lproj (Catalan). The solution for me is add the language using Xcode. Go to Xcode Project, and then in Info Tab add the langugages. Automatically will create the folders nl-NL.lproj and ca-ES.lproj.
Then you can try this on Simulator (Xcode version 4.6) and these languages don't word, instead will load English language.
First rename manually the folder nl-NL.lproj to nl.lproj.
Finally go to file {MY_PROJECT}.xcodeproj/project.pbxproj and replace the PATH of the following lines:
C3D2888817108DDA00CE8AC2 /* nl-NL */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "nl-NL"; path = "nl.lproj/Localizable.strings"; sourceTree = "<group>"; };
C3D2888917108DE500CE8AC2 /* nl-NL */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "nl-NL"; path = "nl.lproj/InfoPlist.strings"; sourceTree = "<group>"; };
That's all, now I can get these languages settings correctly.

NSURLConnection returns NSError with only english as language?

If i turn on Airplane mode on my iPad and trying to make a NSURLConnection i will get an error "The Internet connection appears to be offline." If i change to any other language then english on my iPad i will still have the same error text. Shouldn't i get the error in the current language that is choosen?
Using this code:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"Error: %#",[error localizedDescription]);
}
I found the answer to my question, and since it's related to this question, I'm sharing it so that it can help if someone else needs it.
The file containing error codes and localized descriptions, is located in /System/Library/Frameworks/Foundation.Framework/Resources/en.lproj/FoundationErrors.strings
Copy this file and paste somewhere else (to avoid messing up with the original file). Now run the command in terminal to convert the binary file to a text file:
plutil -convert xml1 FoundationErrors.strings
Now drag the file into your Xcode project and right click and select Open as... Property list. Now you have the keys and the descriptions. You can translate (if your desired language isn't available in localization) or customize them as you want, and save it with a desired name which is Persian.strings in my case.
Now add this code to the method where you want to use it:
//persian error messages file full path
NSString * persianErrorsListFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"Persian.strings"];
//Load file as dictionary
NSDictionary * persianErrorsList = [[NSDictionary alloc] initWithContentsOfFile:persianErrorsListFile];
NSString *errorKey = [NSString stringWithFormat:#"Err%ld", (long)errorCode];
NSString *errorMessage = persianErrorsList[errorKey];

Localization of header files iOS

Is it possible to localize a header with macros in Xcode?
Lets say for English i might want a size of a font to be 17.0f, but 13.0f for Spanish.
Can this be done?
You can anyway put a PLIST file (let's say "constants.plist") in your .lproj localized folders (put the PLIST file aside you Localizable.strings files, in en.lproj/fr.lproj/es.lproj/...).
The PLIST may then contain an NSDictionary of key/value pairs for each value you need to customize according to the user locale (e.g. your font size).
Then you can use:
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"constants" ofType:#"plist"]; // will return the path of the plist in the right language-specific .lproj directory)
NSDictionary* constants = [NSDictionary dictionaryWithContentsOfFile:plistPath];
float fontSize = [[constants objectForKey:#"fontSize"] floatValue]; // or whatever key you use in your plist for this constant
This is then very easy to have a different constants.plist for each language of your app.
Figured it out. What I asked for is not possible. The header files are evaluated during build time but the localization is set during run time.

Localization of strings in static lib

I have a project that uses a static library (SL). In that SL, there are a couple of strings I'd like to localize and the project includes all of the localization files. The localization works just fine when storing all text translations in the same file. The thing is that I'd like to separate the SL strings from the other strings. I have tried to put two different *.strings files (Localizable.strings and Localizable2.strings) in the language folder of interest but that did not work. I have also tried to use two *.strings file with the same name (Localizable.strings) but with different paths. It didn't work either. It seems that only one localization file is supported, right? Could anyone suggest a good way of doing this? I'm using SDK 3.2 beta 2.
It is not possible to bundle it in a static lib, but you can create new bundle like "MyStaticLibraryName.bundle", put inside all localizations and use the code below instead "NSLocalizedString()". All you need to do: add a static library and resource bundle.
NSString *MyLocalizedString(NSString* key, NSString* comment) {
static NSBundle* bundle = nil;
if (!bundle) {
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"MyStaticLibraryName.bundle"];
bundle = [[NSBundle bundleWithPath:path] retain];
}
return [bundle localizedStringForKey:key value:key table:nil];
}
Putting files with the same name intro one project never works, because in the resulting app they end up all in the same location. (Xcode doesn't preserve your directory structure.)
But you can put part of your localization into Localizable2.strings and then use:
NSLocalizedStringFromTable(#"key", #"Localizable2", #"")
Make the localizable string for the static library, then place that string file in a folder "YourLibraryResource".
Rename the folder "YourLibraryResource.bundle".
Now you include this bundle also in the project along with the library. Then use the code given by abuharsky.

Parsing a .txt or html file into a string in the iPhone SDK

So, what I'm trying to do, is take a .txt or html file, being able to search through it, and grab a piece of text from file, place it into a string and finally adding it into a textView.
Each couple of piece of text will be divided like this:
001:001 Text1
001:002 Text2
001:003 Text3
002:001 Text1a
002:002 Text1b
... and so on
So essentially you would search the text for those numbers, and it would grab the text only. Is there a way to do that using objective C and using it on a iPhone app?
Use
NSString *pathToDefaultPlist = [[NSBundle mainBundle] pathForResource:#"TextFile" ofType:#"text"];
to load text file. Then:
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
this function to load the text file in String. Then use NSString function to divide the token :)
In addition to the [NSString stringWithContentsOfFile:encoding:error] to get the text of the file, your textView should have a setStringValue method.
so I'd do something like this:
NSString *pathToTextFile;
NSError *readError;
NSString *fileData = [NSString stringWithContentsOfFile:pathToTextFile
encoding:(appropriate encoding for your file)
error:*readError]
[textView setStringValue:fileData];
This might need a little massaging, I'm at work and don't have my Mac to verify the method signatures etc. But that's the general idea.