igorkulman / iOSLocalizationEditor

Simple macOS editor app to help you manage iOS and macOS app localizations by allowing you to edit all the translations side by side
MIT License
1.46k stars 116 forks source link

Text in cells should not appear escaped #50

Closed tempelmann closed 5 years ago

tempelmann commented 5 years ago

Might be related to #46 - I see the same issue with single quotes, at least in v2.1 (can't use the latest because I'm on 10.13 and can't build for Swift 5).

Apart from that - great tool! Thanks for making it available. I am using it for a macOS app. Meaning you could advertise this to be useful for both Apple platforms, not just for iOS.

I also added a link to here on quora, where I ended up first, looking for this kind of tool: https://www.quora.com/What-is-the-best-translation-tool-for-Mac

igorkulman commented 5 years ago

Could you post a sample strings file? I will check it out.

tempelmann commented 5 years ago

Simple as this:

"example" = "It\'s its";

Appears as "It\'s its" in the editable cell, but should appear as "It's its".

(I had to enter the text here with two backslashes so that they appear as a single backslash. In the original strings file, it's a single backslash and a single quote)

tempelmann commented 5 years ago

Oh - the same issue appears with \? (backslash + question mark). There may be more. Basically, you should scan any sting for backslashes, and whenever you encounter one, you need to handle the next char specially, and then remove the first backslash. The algorithm for handling the next char would be: If it's "n" or "r", create a LF. Otehrwise, just keep the char (so that \n turns into a LF, and \\ turns into \, and \x turns into x, and so on). Of course, you then need to re-escape the chars when writing them back into the strings file. Here, I believe you need to escape any of these chars:

LF -> \n \ -> \\ ? -> \? " -> \" ' -> \'

There may be more, so best to keep them in a list (array) that is easily expandable.

tempelmann commented 5 years ago

I also see a few other small issues (the document does not show when it's "dirty" (i.e. modified), the Open menu should use "…", menu should be uppercase: "Select All", and so on. Small stuff, but attention to detail is important for a tool that's used for getting these things right :)

I'll set it up on my other Mac that runs 10.14 so that I can supply some changes.

igorkulman commented 5 years ago

All PRs are welcome, even for small stuff like adding "..." to a menu item.

igorkulman commented 5 years ago

I did some research and experiments and it looks like only double quotes need to escaped and double quotes like "test" = "Testing \"quotes\""; work properly in the app.

You are right that "example" = "It\'s its"; does not work properly, but you do not have to escape the single quote. If I use "example" = "It's its"; in a real iOS app, it gets handled properly, same with a question mark or a backslash.

I would prefer not to handle escaping of stuff that is not necessary in the app.

tempelmann commented 5 years ago

Thanks for doing the research. I agree that escaping of single quotes is not necessary, but some tools write them regardless. That's why you should still handle them.

Also, assuming you do not have to unescape everything is a common rookie mistake (I've seen this many times). Consider this:

You have to escape the backslash. So, if the user enters \ into a text field, you need to write it as \\. Agreed?

Now, to perform this replacement, you need to find any \ and convert it into \\. But what if you leave other escaped strings in the text, like \?? You'd end up writing it back as \\?, which is equally wrong.

That's why you need to do what I wrote earlier: You need to unescape ANY occurance of \, but you do not need to re-escape any but the required ones (which are double quote, CR and LF, at least).

Okay?

I have posted an unescaping function here, though it's in ObjC, sorry: https://stackoverflow.com/a/56273342/43615

igorkulman commented 5 years ago

Yes I agree that the app should not show escaped character with "\" even if the escaping in the string file is not needed but is there.

I cannot really read Obj-C, Swift version would be better.

tempelmann commented 5 years ago

Isn't it quite easy to include ObjC files into Swift? The code I posted could be added as an ObjC class and then you can call it from Swift, I believe. I haven't done this myself, and I'm currently super busy with my own project code, though once I start working on the localization I might pull your project and try to add this.

igorkulman commented 5 years ago

Integrating a ObjC file is not a problem. The problem is I just do not want to copy paste a block of code I cannot even read to the project.

igorkulman commented 5 years ago

Take a look at #55, ideally try it on your data, works on some escaped strings I tried.