Localizing dynamic NSString - iphone

Going through number of examples regarding to NSLocalizedString, what I found was we need to pre-define all the string in Localized.string file for what-ever language you want to localize. But, is it possible to localize dynamic string. My idea was, I am displaying few text in UILabel that i get after web request. It means the string is now dynamic in nature.

Declare in Localizable.strings
"SAMPLE_LOCALIZE_STRING" = "This is sample dynamic localize string for %#.";
Use it like this
NSString *dynamicStr = #"Test";
label.Text = [NSString stringWithFormat:NSLocalizedString(#"SAMPLE_LOCALIZE_STRING", nil), dynamicStr];

If those strings are fixed ones (I mean a limited number of options) then pre-store them in the localized string file.
If not, I would suggest to add a parameter to your request that would indicate the language and then the server would return string in that language.

I have handled this situation as follows,
Include language in the request. For ex: http://yourIp/language/notesandcondition
The webservice should be designed to handle for different languages.

[NSString stringWithFormat:NSLocalizedString(#"Table View Cell Row %d", #""), indexpath.row];

Related

Different font for parameters in NSLocalizedString

I want to give a label a text that have multiple fonts in it. This can be accomplished by creating a NSMutableAttributedString. However, I am not sure how I format the following case:
String(format: NSLocalizedString("%# has replied in '%#'", comment: ""), username, conversationTitle)
I want to give the username and conversation title a separate font. I want to do this in the less buggiest way. What I mean by this:
I do not want to find out the username later on in the string by using a substring. This is causing issues when the conversationTitle is the same as the username, or the conversationTitle is in the username etc. etc..
I do not want to build up the string, as seen here: https://stackoverflow.com/a/37992022/7715250. This is just bad when creating NSLocalizedString's, I think the translators are going to have a bad time when string are created like that.
Questions like: Making text bold using attributed string in swift, Are there approaches for using attributed strings in combination with localization? and others are mostly string literals without NSLocalizedString or NSLocalizedString with parameters.
First, you should have in your .strings a much more generic and readble key, something like:
"_REPLIED_IN_" = "%# has replied in %#";
Do not confuse keys and values as you seem to do in your example.
Also, it's easier later to see when there is an hardcoded string not localized in your code.
Now, there is an issue, because in English, it might be in that order, but not necessarily in other languages.
So instead:
"_REPLIED_IN_" = "%1$# has replied in %$2#";
Now, I'll use the bold sample, because it's easier, but what you could do is use some custom tags to tell you that it needs to be bold, like HTML, MarkDown, etc.
In HTML:
"_REPLIED_IN_" = "<b>%1$#</b> has replied in <b>%$2#</b>";
You need to parse it into a NSAttributedString:
let translation = String(format: NSLocalizedString(format: "_REPLIED_IN_", comment: nil), userName, conversationTitle)
let attributedText = NSAttributedString.someMethodThatParseYourTags(translation)
It's up to you to choose the easiest tag format), according to your needs: easy to understand by translators, and easy to parse (CocoaTouch already has a HTML parser, etc.).

iPhone app: emoji unicode

I have a Json return that has a string that sometimes inludes something like \Uf604 in the array (IE memo = "\Uf604";). I need to convert it to \U0001F604 if possible.
I tried to do something like stringByReplacingOccurrencesOfString but at that point when its in a string and it's been converted to ÔòÑ which I think it needs to be üòÑ to be displayed as a emoticon. I also tried
[str stringByReplacingOccurrencesOfString:#"Ô" withString:#"ü"];
But that didn't change anything. It still gets returned as ÔòÑ.
Any help would be appreciated!
I believe str = [str stringByReplacingOccurrencesOfString:#"Ô" withString:#"ü"]; is what you are trying to do.
stringByReplacingOccurrencesOfString:withString: doesn't change the string you are calling on but returns a new string with replaced characters.

supporting internationalization for NSString's

I have a bunch of the following line of code:
[NSString stringWithFormat:#"%# and %#", subject.title, secondsubject.title];
[NSString stringWithFormat:#"%# and %d others", subject.title, [newsfeeditem count] - 1];
and a lot more in the app. Basically I am building a news feed style like facebook where it has string constants. blah liked blah. Where/how should I do these string constants so it's easy to do for internationalization? Should I have a file just for storing string constants?
See the String Resources section of the Resource Programming Guide. The key section for this particular problem is "Formatting String Resources."
You'd have something like:
[NSString stringWithFormat:NSLocalizedString(#"%1$# and %2$#", #"two nouns combined by 'and'"),
subject.title, secondsubject.title];
The %1$# is the location of the first substitution. This lets you rearrange the text. Then you would have string resource files like:
English:
/* two nouns combined by 'and' */
"%1$# and %2$#" = "%1$# and %2$#";
Spanish:
/* two nouns combined by 'and' */
"%1$# and %2$#" = "%1$# y %2$#";
You need to be very thoughtful about these kinds of combinations. First, you can never build up a sentence out of parts of sentences in a translatable way. You're almost always need to translate the entire message in one go. What I mean is that you can't have one string that says #"I'm going to delete" and another string that says #"%# and %#" and glue them together. The word order is too variable between languages.
Similarly, complex lists of things can cause all kinds of headaches due to various agreement rules. Some languages have special plural rules, gender agreements, and similar issues. As much as possible, keep your messages simple, short, and static.
But the above tools are very useful for solving the problem you're discussing. See the docs for more details.

iphone: detecting and replacing a URL in an NSString

I currently have an NSString which can take in a message body similar to a tweet.
E.g.
NSString *sampleText = "This text contains the link http://www.google.com"
I need to write a function that can take in this text, detect that a url exists in the string, and be able to replace the url with a placeholder text.
E.g. after the function is used, the text should equal:
sampleText = "This contains contains a LINK"
Can someone please tell me how I can do this? Do I need to use RegEx?
iOS 4 has [NSRegularExpression replaceMatchesInString:options:range:withTemplate:] which looks like a good bet. (It expects an NSMutableString.)

Parsing XML, details and attributes, iphone

I'm not sure about parsing an xml with attributes. have seen a similar question here
But it shows to get an attribute of intValue. But i need to get the attributes of string type,How to do that?? Images of xml and the relevant portions are given in the following links
Click here for xml and here for required data
This answer to the linked question should work for you as well. The contents of attributeDict are already NStrings. All that is going on extra in the linked answer that they are calling the intValue method on the returned NSString to parse that string into an int. In your case, you don't need this little bit of an extra step. If you just do this:
NSString * stringValue = [attributeDict objectForKey:#"attribute"];
you'll have the value of the attribute called "attribute" in a string.