Frage

I have a HTML string that needs to be localized; the only problem is that it has parameters, like this:

    NSString *htmlString = [NSString stringWithFormat:NSLocalizedString(@"<html><head>"
                        "<style type=\"text/css\"> body {font-family: \"Verdana\"; font-size: 12;} </style></head>"
                        "<body><h2>%@ %@</h2><p>email: %@<p>phone: %@<p>services: %@<p><img src=\"%@\"/>"  
                        "</body></html>",nil),
                        client.aClientFirstName,
                        client.aClientLastName,
                        client.aClientEMail,
                        client.aClientPrimaryPhone,
                        appt.aServices,
                        fileURL];

The problem is that the "key" never contains the same data, and therefore will never match. This works fine when working with 'en' (because it defaults to that when it can't find a match), but all other languages fail when it gets displayed in a UIPopover. The question is: is there a way to code around this so no matter what the 'client' info is, it will display properly for each localization?

War es hilfreich?

Lösung

In your Localizable.strings files, you should create a separate fixed key and put the html template as the value that contains the placeholder for the parameters, for example:

"HTML_STRING" = "<html><head><style type=\"text/css\"> body {font-family: \"Verdana\"; font-size: 12;} </style></head><body><h2>%@ %@</h2><p>email: %@<p>phone: %@<p>services: %@<p><img src=\"%@\"/></body></html>";

And then use the key in your code, for example:

NSString *htmlString = [NSString stringWithFormat:NSLocalizedString(@"HTML_STRING",nil),
                    client.aClientFirstName,
                    client.aClientLastName,
                    client.aClientEMail,
                    client.aClientPrimaryPhone,
                    appt.aServices,
                    fileURL];

Here your htmlString variable will give you the localized version of the string with parameters included.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top