Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to display numbers in Arabic language?
Started by alzaabi98 Oct 18 2013 05:44 AM

- - - - -
21 replies to this topic
display newtext arabic language

Best Answer jonjonsson , 21 October 2013 - 05:42 PM

Try this (requires the utf8_decode function)

 

local function numbersToArabic(numberString)
        
        local numberString = tostring(numberString)
        local replacements = {["1"] = "Ù¡", ["2"] = "etc", ["3"] = "etc"}
        return utf8_decode(string.gsub (numberString, "%d", function (str) return replacements [str] or str end))
        
 end
    
print(numbersToArabic("You scored 11"))
 

 

You have to add to the replacements table of course.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Hi Experts,

 

I am writing an Arabic application. I wonder how can i write the numbers in Arabic when i am using the display.newText because I want to display the result in Arabic numbers not English since it is Arabic app. I need you support ..

 

i am attaching picture to show the Arabic numbers for whom who don't know them. 

 

 

 

 

Regards

Abdulaziz

Attached Thumbnails

  • arabic numbers.PNG


[TOPIC: post.html]
#2

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

If your file is saved in utf8 format (without BOM) and your font supports arabic characters then you can just pass string with arabic text to display.newText

[TOPIC: post.html]
#3

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Piotrz55,
Can you please explain more your answer? Which files do you mean.. i appreciate if you can write simple example..
Also for your informatiom i can pass arabic text and it works thru display.nextText.. only i cant type arabic numbers as they come in english even i am typing in arabic.
Regards
Abdul

[TOPIC: post.html]
#4

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

I cannot write sample be because there is no sample as such. When you create file your editor have options to save file in different encodings: ascii, utf8... To support not english characters you must save file in utf8 format (I'm not talking about .lua extension).

You mean you want to have your arabic '1' instead of 1 in code? If so then not possible, only english syntax and latin numbers.

If you want to have arabic '1' displayed on screen then just pass it as string parameter to newText

[TOPIC: post.html]
#5

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Hi Piotrz55,

 

I saved the file as you mention in utf8 format using windows notepad but still i am not able to show Arabic numbers displayed. I don't want to use the Arabic numbers in the code. I want to send it as text only if possible. 

 

the issue when i type "1" .. it comes as English only... where as the Arabic letter comes perfectly in the screen.. the issue in the numbers only.

 

any idea  ???

 

Regards

Abdul



[TOPIC: post.html]
#6

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

When you type "1" then it english one not arabic! You must pass arabic character for one - as one-character string.

[TOPIC: post.html]
#7

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

I suggest you to use notepad++ at least and change file encoding to utf8(without BOM). Then save it.

[TOPIC: post.html]
#8

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Hi Piotrz55,

 

I appreciate your quick response. I tried doing that but I was not able to type Arabic numbers within the file nor in the notepad for testing. I found something interesting , when I change the settings for regional settings in the control panel to set the "use native digits"  = National.. every number in my  PC changed to Arabic.. even inside lua code. when I run the lua file i got Arabic numbers also displayed.  This works but not the way I want it because I don't want the Arabic number to show in the code because it is confusing, 

 

I don't what else I can do in this issue. but i am successful to do it in notepad then i am sure it will work within lua file..

 

 

Regards

Abdulaziz



[TOPIC: post.html]
#9

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

I tried doing that but I was not able to type Arabic numbers within the file nor in the notepad for testing

You cannot type arabic characters in editors (only english and ones your keyboard supports).  First you must translate some text and then paste it into string in your code.



[TOPIC: post.html]
#10

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Just to clarify ,.,, i am able to type Arabic words within display.newtext and it works perfectly in the phone or simulator.. the issue with numbers only.. I think the numbers needs to manged differently if you want to display them in different language other than English.

 

the default is always English numbers even i am typing Arabic language .. !!!

 

please see the picture that shows the issue :

 

arabic_numbers.png



[TOPIC: post.html]
#11

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

Ah, didn't know you have keyboard supporting arabic characters. So if your keyboard or editor supports only "1" (as used in english but generaly speaking: 1, 2, 3, 4, 5,... are called "arabic numerals" :D ) then only some copy-paste from external source will do for numbers in string format  :)



[TOPIC: post.html]
#12

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

:)  :)  :)

 

it didn't work.. i copied the  Arabic number  from MS Word ( in MS word called Hindi).. ar_numbers.png but when i paste in outlaw it showed as English ... outlaw_paste.png

 

My friend . I think this something might be new for Corona SDK as i don't see Arabic developers yet.. this might come to corona sooner or later..

 

 

My app is like a game for kids to select letters and get some  credit.. at the end i want to show them the number of how much they got. At the same time i want to show it in Arabic format ( some people called it Hindi like in MS word )..

 

Even if string works then it will not help me much.. for even string is not working.. if at least string works then we might think to format the result number and do some tricks to show it in Arabic  :(  :(  :(  :(  :(



[TOPIC: post.html]
#13

jonjonsson

[GLOBAL: userInfoPane.html]
jonjonsson
  • Corona Geek

  • 1,051 posts
  • Corona SDK

I don't know what is the cuase but this may be a workaround: http://developer.coronalabs.com/code/utf-8-encode-and-decode

 

First go to http://www.cafewebmaster.com/online_tools/utf8_encode

 

Copy some arabic numbers to the form and choose UTF-8 encode.

 

Take the result and put in newText

 

local test = display.newText(utf8_decode("Ù¡â - Ù¢â - Ù£â - Ù¤â - Ù¥â - Ù¦â - Ù§â"), 0, 0, native.systemFont, 10)

 

And you should see arabic numbers. 



[TOPIC: post.html]
#14

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

jon,

 

i will try to make what you suggest and i see if i can make.. also i found the below link which explain something similar but in Java..

 

please have a look

 

http://stackoverflow.com/questions/1675786/convert-from-english-digits-to-arabic-ones-in-html-page/1675965#1675965



[TOPIC: post.html]
#15

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Perfect ,, 

 

I am able to display number 11 in Arabic using the following :

local test = display.newText(utf8_decode("١١"), 0, 0, native.systemFont, 55)

 

Now how can I convert the calculated value let us say result in Arabic coding and then use the above function to display it.

 

for example :

 

result = 200 

 

ar_number_2.png



[TOPIC: post.html]
#16

jonjonsson

[GLOBAL: userInfoPane.html]
jonjonsson
  • Corona Geek

  • 1,051 posts
  • Corona SDK

  Best Answer

Try this (requires the utf8_decode function)

 

local function numbersToArabic(numberString)
        
        local numberString = tostring(numberString)
        local replacements = {["1"] = "Ù¡", ["2"] = "etc", ["3"] = "etc"}
        return utf8_decode(string.gsub (numberString, "%d", function (str) return replacements [str] or str end))
        
 end
    
print(numbersToArabic("You scored 11"))
 

 

You have to add to the replacements table of course.



[TOPIC: post.html]
#17

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Jon,,

Thanks Man for your help ,, it works using the following

local function numbersToArabic(numberString)
       
        local numberString = tostring(numberString)
        local replacements = {["0"] = "Ù ",["1"] = "Ù¡", ["2"] = "Ù¢", ["3"] = "Ù£",["4"] = "Ù¤",["5"] = "Ù¥",["6"] = "Ù¦",["7"] = "Ù§",["8"] = "Ù¨",["9"] = "Ù©"}
        return utf8_decode(string.gsub (numberString, "%d", function (str) return replacements [str] or str end))
       
end

------------------
ToArabicNum=numbersToArabic("0123456789")
local test = display.newText(ToArabicNum, 0, 0, native.systemFont, 55)


I have one question just to understand the concept for the conversion. What is the difference between the coding we used from the website you provided (http://www.cafewebmaster.com/online_tools/utf8_encode ) and the one I found (http://www.utf8-chartable.de/unicode-utf8-table.pl).

in your website number 1 in arabic is encoding as "Ù¡"   whereas in the second website it is encoded as "U+0661" see the picture below for arabic encoding ,, when i used these encodes with functions you prvoided it didnt work..
 

 

 

Attached Thumbnails

  • arabic numbers.PNG
  • utf encoding for arabic numbers.PNG


[TOPIC: post.html]
#18

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

"Ù¡" and such is probably because editor or editor's font do not support this unicode characters (if it would you would see arabic ones) and tries to somehow display this character using other knows signs.

 

Ù¡ is how your editor reads what in unicode table is stated as U+0661 but when you write U+0661 manualy then it doesn't interpret it as unicode character. 

 

You just have to stick to your method :)



[TOPIC: post.html]
#19

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Thanks Both Jon and piotrz55,,,

 

I appreciate your responses and explanation until we read to the solution. I am still wishing to see more explanation in this topic or similar ones because it will really help many people who use different languages... Also I marked this as solved .,, :)

 

Regards

Abdulaziz



[TOPIC: post.html]
#20

danialabdl90

[GLOBAL: userInfoPane.html]
danialabdl90
  • Observer

  • 1 posts
  • Corona SDK

Thanks Both Jon and piotrz55,,,

 

I appreciate your responses and explanation until we read to the solution. I am still wishing to see more explanation in this topic or similar ones because it will really help many people who use different languages... Also I marked this as solved .,, :)

 

Regards

Abdulaziz

hi. im from iran and this post helped me too. thanks.

danial abdl.



[TOPIC: post.html]
#21

hesam.tajbakhsh

[GLOBAL: userInfoPane.html]
hesam.tajbakhsh
  • Observer

  • 1 posts
  • Corona SDK

Jon,,

Thanks Man for your help ,, it works using the following

local function numbersToArabic(numberString)
       
        local numberString = tostring(numberString)
        local replacements = {["0"] = "Ù ",["1"] = "Ù¡", ["2"] = "Ù¢", ["3"] = "Ù£",["4"] = "Ù¤",["5"] = "Ù¥",["6"] = "Ù¦",["7"] = "Ù§",["8"] = "Ù¨",["9"] = "Ù©"}
        return utf8_decode(string.gsub (numberString, "%d", function (str) return replacements [str] or str end))
       
end

------------------
ToArabicNum=numbersToArabic("0123456789")
local test = display.newText(ToArabicNum, 0, 0, native.systemFont, 55)

I have one question just to understand the concept for the conversion. What is the difference between the coding we used from the website you provided (http://www.cafewebmaster.com/online_tools/utf8_encode ) and the one I found (http://www.utf8-chartable.de/unicode-utf8-table.pl).

in your website number 1 in arabic is encoding as "Ù¡"   whereas in the second website it is encoded as "U+0661" see the picture below for arabic encoding ,, when i used these encodes with functions you prvoided it didnt work..
 

I have same problem and when I try to use this code I got an Error that says "attempt to call global 'utf8_decode" (a nil value). What should I do?



[TOPIC: post.html]
#22

jonjonsson

[GLOBAL: userInfoPane.html]
jonjonsson
  • Corona Geek

  • 1,051 posts
  • Corona SDK

I have same problem and when I try to use this code I got an Error that says "attempt to call global 'utf8_decode" (a nil value). What should I do?

 

You need the UTF-8 decode function as well: http://pastebin.com/t2aQpMdd




[topic_controls]
[/topic_controls]