Search code examples
iphoneipadxcode4

Localized XML files


I have an XML file in English that I translated into Italian and French.

In my project xcode4 I have added them but because the files have the same name I do not know how to call them from code.

For strings there is NsLocalizedString but for files how do you accomplish this?


Solution

  • See the NSBundle reference:

    pathForResource:ofType:

    Returns the full pathname for the resource identified by the specified name and file extension.

    The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. (In Mac OS X, this directory is typically called Resources but in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (The search order for the language-specific directories corresponds to the user’s preferences.) It does not recurse through other subdirectories at any of these locations. For more details see Internationalization Programming Topics.

    (emphasis added)

    So you’d use:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"xml"]
    

    Provided that your files are localized using the localization tool in Xcode’s file inspector which automatically puts the files into the correct *.lproj folder.