iOS: Localise multi-line string literals - ios

Since a recent swift version, multi line string literals are available that allow to format the appearance of a multi line string easily.
I'm looking for a way, though, to localise such a string.
Here is an example of the text of a pre-configured mail, that users can send:
mailComposerVC.setMessageBody("""
Hi,
I would like to share the following feedback:
""",
isHTML: false)
It seems there is no way to properly transform this into the localisable .strings file.
As a workaround, I came up with the solution to individually localise each part of the message and use interpolation:
let localisedGreeting = NSLocalizedString("Hi", comment: "")
let localisedMessage = NSLocalizedString("I would like to share the following feedback: ", comment: "")
mailComposerVC.setMessageBody("""
\(localisedGreeting),
\(localisedMessage)
""",
isHTML: false)
The .strings file looks like this:
"Hi" = "Hallo";
"I would like to share the following feedback: " = "ich möchte folgendes Feedback geben: ";
Is there a better/more concise way to do this?

Both keys and values in the Localizable.strings file can be multi-line.
With
"Hi,
I would like to share the following feedback:
" = "Hallo,
ich möchte folgendes Feedback geben:
";
the call
let localizedMessage = NSLocalizedString("""
Hi,
I would like to share the following feedback:
""", comment: "")
expands as intended.
This might however not work with localization tools, and also does not
display with the correct syntax coloring in the Localizable.strings, which
might be confusing.
I would use a short (single-line) key instead, and localize that for all
languages (including the default language english).

For me this layout of text and quotes worked (Swift 5.3, Xcode 12)
Localizable.strings
"First Line
Second Line" = "Erste Linie
Zweite Linie";
Implementation
let lines = NSLocalizedString("""
First Line
Second Line
""", comment: "")

Related

Is there any way to replace double quotes with a backslash in swift

I have a submit form where there are multiple textfields.
Whenever user enters text like "Hi, my name is "xyz"", the service does not accept this JSON due to double quotes in my string.
Please suggest ways to escape this character.
I have tried using encode and decode JSON, replaceOccurrencesOf methods, but none work.
replaceOccurrencesOf()
The below code snippet with replace "(double quote) in a string by \". This will help to replace "(double quote) by any string or character in a given string.
Swift 5 or above
let replacedString = stringToBeModified.replacingOccurrences(of: "\"", with: #"\""#)
Instead of putting the name (i.e., "XYZ" if you getting xyz from textfield ) why not to place (textField.text!) it will not put extra " "

Creating hyperlink from text in Swift

I have a json file which has all the information in text.
One of the node in json looks like this :
"description": "File. Verify File"
What I would like to do is create a hyperlink in a UILabel textfield in swift like :
File ***Verify File***
I tried to do lookup on stackoverflow but nothing was concrete available. I wrote the following code to get the "href" text using Regex.
let regexOptions:NSRegularExpressionOptions?
= NSRegularExpressionOptions.CaseInsensitive
let regex = try NSRegularExpression(pattern: "<a[^>]+href=\"(.*?)\"[^>]*>.*?</a>", options: regexOptions!)
And then use NSMUtableAttributedString to create a link of it. Am I in the correct direction ?
Yes, it's a good approach but you will work more than if your json only have the essential information: { "description": link" } so you can use a UIButton with a #IBAction attached to open a WebController or something like that with the link.

Xcode is throwing me an error in swift

I'm following a tutorial(http://youtube.com/watch?v=xvvsG9Cl4HA 19 min 20sec) and to make his code look neat he puts some on a ew line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \
(myPlacement.country) \
(myPlacement.postalCode)"
}
. But when I try I get an error
unterminated string literal
and
consecutive statements on a line must be seperated by a ';'
but the guy in the tutorial has done it the exact same way. What's going on?
I'm using the latest swift and and latest xcode 7.2 any help would be apreciated
if I write everything on the same line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
}
it works fine though
if I write everything on the same line like this
Well, there's your answer. You are not permitted to break up a string literal into multiple lines as you are doing in your first example. There are languages that permit this, but Swift is not one of them. This is not legal:
let s = "hello
there"
There is no magic line-continuation character which, placed at the end of the first line, would make that legal.
If the window is narrower than the line, the editor may wrap the line, for display purposes; but you cannot put actual line breaks inside a string literal.
You can work around this by combining (concatenating) multiple string literals, if you think that makes for greater legibility. This, for example, is legal:
let myAddress = "\(myPlacement.locality) " +
"\(myPlacement.country) " +
"\(myPlacement.postalCode)"
I look your video tutorial carefully. You have a misunderstanding here.
You must pay attention to the video, the code in this picture is not break lines because he add a return here, it is because his screen is too narrow.
So, the real code is
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
Please watch it carefully.
And you may need know first, \ in \(myPlacement.locality) is a escape character, it means to get the value of myPlacement.locality and put in the string.

Cleaning a URL with PHP

I've been looking though google and stackflow for an answer for this and testing a few finds but I still can't get this working.
All of these end my link at a space. For example www.website.com/movies/movie
Where I'm trying to get it to read www.website.com/movies/movie with spaces here.mp4
$namehref = "movie/" . $dirArray[$index]. " download";
$DoStream = "Watch";
$DoDownload = "Download";
However this code does not remove the spaces???
$name = $dirArray[$index];
$movienameonly = substr($name, 0, -4);
example www.website.com/movies/movie with spaces here
So my questions are - Why does the first section of code remove the spaces and how do I correct it. In addition to spaces I also hit errors with 's as well.
example They're here.mp4
To remove the spaces completely:
preg_replace("/\s/", "", $your_url);
To replace the spaces with %20 (best way):
preg_replace("/\s/", "%20", $your_url);
To replace spaces with + like url_encode($url) does:
preg_replace("/\s/", "+", $your_url);
To replace ' with %27:
preg_replace("/\s/", "%20", $your_url);
You get errors because spaces can't be inputted in the browser and converts the spaces to %20 and the apostrophe to %27
I found it:
$DoStream = "Watch";
Should have been
$DoStream = "<a href='$the_dir'>Watch</a>";

HTML Styles in TextView on Mono for Android

I have this situation in my strings.xml.
<string name="mensagem">Olá <b><i> {0} </i></b>,</string>
And in my code I do this:
string msg = String.Format(Resources.GetString(Resource.String.mensagem).ToString(), cliente.Nome.ToUpper());
lblNome.Text = Html.FromHtml(msg).ToString();
But the style tags (b,i) do not work. I need to concatenate words with and without style, so I need to do it in this way. I cant use setTypeface because I need to style individual words and after that, join those words on a sentence.
What is the way ?
Regards,
Marcelo.
You need to change your code to the following.
string msg = String.Format(Resources.GetString(Resource.String.mensagem).ToString(), cliente.Nome.ToUpper());
lblNome.TextFormatted = Html.FromHtml(msg);
Html.FromHtml(string).ToString() is just converting the formatted ISpannable back to a string so you are losing the formatting.

Resources