os.date() returns date formatted using english locale

Hi,

no matter on the system settings, whether it’s on simulator [on mac] or on a device [android] the date is always formatted using english locale [so I get Nov instead of Lis for example].

Is there any way to fix this, or do I have to do it on my own?

If I have to do it on my own, do you know any reasonable way to get the date formatted as per locale?
I don’t want users to have weird date formatting, for example US date format [month before day] makes no sense to me.

Thanks,
Krystian [import]uid: 109453 topic_id: 32800 reply_id: 332800[/import]

A possible reasonable way to format depending on locale; http://monkeybin.no/blog/archives/2011/09/18/corona-sdk-localization-made-easy/ (Have not used this but many users praise the post, it’s linked to frequently.)

I get the day/month VS month/day confusion - I have to use the US format but find day/month makes a lot more sense :wink: [import]uid: 52491 topic_id: 32800 reply_id: 130497[/import]

A possible reasonable way to format depending on locale; http://monkeybin.no/blog/archives/2011/09/18/corona-sdk-localization-made-easy/ (Have not used this but many users praise the post, it’s linked to frequently.)

I get the day/month VS month/day confusion - I have to use the US format but find day/month makes a lot more sense :wink: [import]uid: 52491 topic_id: 32800 reply_id: 130497[/import]

Hi Peach,

thanks for taking the interest.

We are using something similar for i18n, however I was wondering about the dates.
I guess I’ll just create a separate date formatting string for every language I want to support and put it as another locale entry.
Thanks again,
good that we have you here.

Krystian [import]uid: 109453 topic_id: 32800 reply_id: 130531[/import]

Hi Peach,

thanks for taking the interest.

We are using something similar for i18n, however I was wondering about the dates.
I guess I’ll just create a separate date formatting string for every language I want to support and put it as another locale entry.
Thanks again,
good that we have you here.

Krystian [import]uid: 109453 topic_id: 32800 reply_id: 130531[/import]

Hi,

Well, if the problem is only about date vs month or month vs date (what should come first), well, I’ll suggest to take a closer look to this.

And, as for localization, the technique described in Monkeybin post is simple, yet you might want to try a dedicated library, named i18n, which handles localization tremendously well. Plus, it features pluralization.

Regards,
Roland. [import]uid: 142361 topic_id: 32800 reply_id: 130962[/import]

Hi,

Well, if the problem is only about date vs month or month vs date (what should come first), well, I’ll suggest to take a closer look to this.

And, as for localization, the technique described in Monkeybin post is simple, yet you might want to try a dedicated library, named i18n, which handles localization tremendously well. Plus, it features pluralization.

Regards,
Roland. [import]uid: 142361 topic_id: 32800 reply_id: 130962[/import]

Hi,

I’m picking up this topic because I have the same problem.

I want to output a date in a localized style. Is there a way to achieve that with Corona SDK?

I tried the following:

os.setlocale(“de”, “time”) – should not be necessary and does not have any influence either

local timestamp = os.time( { year = 2014, month = 1, day = 30 } )

local dateAsString = os.date( “%x”,  timestamp)

print( dateAsString )

However, the output is in english locale format. The device is set on German language as well.

Best regards,

Bjoern

Hi @bjoern,

Have you read through the comprehensive tutorial on working with times and dates? I think you can get this value output as UTC time, then parse it as you see fit. Please read the entire section “Working With Dates” here:

http://coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Sincerely,

Brent Sorrentino

Hi,

are there plans to solve this problem?

print (os.date("%A"))

still outputs i.e. Friday regardless of the language setting of the device.

I’m creating a calendar-app and a table for each possible language is no work-around.

best regards

Dan

The os.date() function returns whatever the native device’s operating system returns for the C library function strftime().  You can google that to learn more about it.  Corona and Lua are just passing this value through to and from strftime().  The work around if you can’t learn how to localize strftime() would be to create your own look up tables:

local dayOfWeek = { “lunes”, “martes”, “miércoles”,“jueves”,“viernes”,“sábado”,“domingo”}

print( dayOfWeek[os.date("%w")+1] )

(The +1 is the fact that %w returns 0-6, afterall C is a 0 based language and you have to add one to adjust to Lua being a 1 based language with arrays).

You can use %m for months (it’s 1-12 based).

Thanks for your reply.

I use look up tables for the localization of the app-menu.

But for outputting the name of weekday/month i was hoping for a more universal and automatic solution to support all possible languages (not just the ones in my LUT).

I thought i could just use “os.setlocale(‘xx_XX’)” (where the identifier ist read from system.getPreference(locale/ui))

This works fine in the corona- and the xcode simulator.

But if the information in the book “Learn Lua for iOS Game Development by Jayant Varm” is right, os.setlocale isn’t supported on the actual device.

Hi,

I’m picking up this topic because I have the same problem.

I want to output a date in a localized style. Is there a way to achieve that with Corona SDK?

I tried the following:

os.setlocale(“de”, “time”) – should not be necessary and does not have any influence either

local timestamp = os.time( { year = 2014, month = 1, day = 30 } )

local dateAsString = os.date( “%x”,  timestamp)

print( dateAsString )

However, the output is in english locale format. The device is set on German language as well.

Best regards,

Bjoern

Hi @bjoern,

Have you read through the comprehensive tutorial on working with times and dates? I think you can get this value output as UTC time, then parse it as you see fit. Please read the entire section “Working With Dates” here:

http://coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

Sincerely,

Brent Sorrentino

Hi,

are there plans to solve this problem?

print (os.date("%A"))

still outputs i.e. Friday regardless of the language setting of the device.

I’m creating a calendar-app and a table for each possible language is no work-around.

best regards

Dan

The os.date() function returns whatever the native device’s operating system returns for the C library function strftime().  You can google that to learn more about it.  Corona and Lua are just passing this value through to and from strftime().  The work around if you can’t learn how to localize strftime() would be to create your own look up tables:

local dayOfWeek = { “lunes”, “martes”, “miércoles”,“jueves”,“viernes”,“sábado”,“domingo”}

print( dayOfWeek[os.date("%w")+1] )

(The +1 is the fact that %w returns 0-6, afterall C is a 0 based language and you have to add one to adjust to Lua being a 1 based language with arrays).

You can use %m for months (it’s 1-12 based).

Thanks for your reply.

I use look up tables for the localization of the app-menu.

But for outputting the name of weekday/month i was hoping for a more universal and automatic solution to support all possible languages (not just the ones in my LUT).

I thought i could just use “os.setlocale(‘xx_XX’)” (where the identifier ist read from system.getPreference(locale/ui))

This works fine in the corona- and the xcode simulator.

But if the information in the book “Learn Lua for iOS Game Development by Jayant Varm” is right, os.setlocale isn’t supported on the actual device.