Handle arabic string in PHP with Eclipse

2019-04-06 06:16发布

I am currently working on the localization of a website, which was first in english only. A third party company did the translations, and provided us with an excel file with the translations. Which I successfully converted to a PHP array that I can use in my views. I'm using Eclipse for Windows to edit my PHP files.

All is fine, except that I need to add variables in my strings, ex:

'%1 is now following %2'

In arabic I was provided with strings like this one:

'_______الآن يتتبع _______'

I find that replacing __ with %1 and %2 is incredibly difficult because the arabic part is a right to left string, and the %1, %2 will be considered left-to-right, or right-to-left, and I'm not sure . I hardly have the results I expect with the order of my param, because %1 will sometimes go to the left of the string, sometimes on the right, depending on where I start to type. Copy-pasting the replacement strings can also have the same strange effects.

Most of the times I end up with a string like this one:

%2الآن يتتبع %1

The %1 should be at right hand site, the %2 at the left hand site. The %1 is obviously considered right-to-left string because the % appears on the right. The %2 is considered left-to-right.

I'm sure someone as this issue before. Is there any way it can be done easily in Eclipse? Or using a smarter editor for arabic issues? Or maybe it is a Windows issue? Is there a workaround?

UPDATE

I also tried splitting my string into multiple strings, but this also changes the order of the parameters:

'%1' . 'الآن تتبع' . '%2'

UPDATE 2

It seems that changing the replacement string makes things better. It is probably linked to how numbers are handled in Arabic strings. This string was edited in Eclipse without any problem. The order of the parameter is correct, the string is handled correctly by PHP:

'{var2} الآن يتتبع {var1}'

If no other solution is found, this could be a good alternative.

3条回答
家丑人穷心不美
2楼-- · 2019-04-06 06:55

Being an Arabic speaker I get lots of localization tasks. Although I haven't faced this problem in particular but I've had many left-to-right/right-to-left issues while editing. I've had success working with Notepad++.

So here's what I usually do when I want to edit Arabic text

  1. Open empty Notepad++ *
  2. Set encoding to UTF-8 (Encoding -> Encoding in UTF-8)
  3. Enable RTL mode (View -> Text Direction RTL)
  4. Paste your strings

And here's a screenshot showing how I'm editing your string

enter image description here

*: for some reason, whenever I open an already existing file things go bananas. So maybe I'm being superstitious, but this has always worked for me.

Update: First time I did this I was skeptical because the strings looked wrong, but then I did this:

print_r(str_split($string));

and I saw that they're indeed in the correct order.

查看更多
等我变得足够好
3楼-- · 2019-04-06 07:03

@Adnan helped me realize and later confirmed that there are issues when mixing Latin numbers with Arabic text.

Based on that conclusion, the solution is simply to stop using %1, %2, %3, ... as placeholders. I will be using more descriptive keywords instead, for example {USER}, {ALBUM}, {PHOTO}, ...

This shows the expected result in the PHP file and it is easily editable:

'ar' => '{USER} الآن يتابع {ALBUM}'
查看更多
Rolldiameter
4楼-- · 2019-04-06 07:10

I would prefer the original Notepad for this kind of task.

  1. Open Notepad, make sure you're in LTR mode
  2. Type %1
  3. Change mode to RTL by pressing CTRL + SHIFT
  4. Paste the arabic string into the editor.
  5. Revert back to LTR by pressing CTRL + SHIFT again.
  6. Type %2
  7. Select all with CTRL + A and copy with CTRL + C
  8. Paste into the IDE. It should look weird but execute as expected.

Reason for using Notepad: More complex editor such as Notepad++, Sublime, Coda (Mac), and some IDEs - in your case Eclipse may not use the correct encoding, and Notepad is simple yet works good for multilangual tasks.

查看更多
登录 后发表回答