Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read String From .strings file in XCode

I am new to iOS and I have a .strings file in which I store a disclaimer that is viewed when my application is opened.

However, I am having trouble figuring out how to call the "disclaimer" string from the disclaimer.strings file.

Help would be appreciated. Also, links to relevant pages are appreciated!

like image 305
hwrdprkns Avatar asked Jun 27 '11 19:06

hwrdprkns


2 Answers

Well, first, rather than using a .string file, if there's only the disclaimer in it, then I would just use a txt file:

NSString * fName = [[NSBundle mainBundle] pathForResource:@"disclaimer" ofType:@"txt"];
if (fName) {
    self.disclaimer = [NSString stringWithContentsOfFile: fName];
}

On the other hand, if you want to do a localized version for each country, then just add it to your "Localizable.strings" file. Your code is just:

self.disclaimer = NSLocalizedString(@"disclaimer", @"Disclaimer text for each country");

Then either use genstrings to collect all your Localizable strings, or create "Localizable.strings" (File\New\New File\ioS\Resource\Strings File), and then you can edit/add in the text:

"disclaimer" = "This is the English version of our disclaimer..."; 

Then you create a new language version of Localizable.strings and edit it with that country's disclaimer.

like image 157
mackworth Avatar answered Oct 11 '22 20:10

mackworth


The naming convention for Localizations expects a Localizable.strings file, under which you can then provide different language versions.

The best way is to start with using statements in your code like this one that returns the localized content for disclaimer :

NSLocalizedString(@"Disclaimer",@"Disclaimer")

The next step is to call genstrings from the command line on your classes directory:

genstrings -o en.lproj *.m
like image 23
werner Avatar answered Oct 11 '22 20:10

werner