Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

How to handle multiple number formats?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Tue Nov 11, 2003 1:12 pm    Post subject: How to handle multiple number formats? Reply with quote

Hi!

I'm thinking about what would be the best way to use different number-formats in my application.

A user can choose what number-format he would like to use. There are different kinds of numbers(not all numbers, but the same problem):

- number-formats
- date-format
- users timezone

In my database I have the "original" data, which means float or int numbers, timstamps of servers timezone...

But how should I implement this? It should follow MVC Design, but what does this mean here?

There are different possibilities:

1. Use a function inside my smarty-templates, perhaps somethink like:
Code:
{$number|user_format}


but in this case, I'm not sure where to specify the settings from user, and to assign the number-format setting as variable zu the template and then use something like

Code:
{$number|user_format:$setting.number_format}


is not nice applicartion design(MVC)

2. I create a function in my PHP-scripts, something like
Code:
function user_number_format ($val) {
  global $user_settings;
  return number_format($val....);
}


and so I have to use this funtion in every loop of a DB-result, with every number. But I also have to implement this in my validation-methodes and "translation-functions" to translate such a number from user-input(forms) into a PHP-readable number-format.

Do you know a better way to do this?

2 additional questions:
- should the user be able to choose a special price-format? Or is it enough to choose the number-format? Because someone says

Code:
USD 234.99


or as in Germany

Code:
234,99 EUR


- which number- and date-formats would you offer and how would you present them?

number-formats:
Code:
###.###,##
###,###.##


date-formats
Code:
DD.MM.YYYY
MM-DD-YYYY


how would you write this and would you offer any additional formats?
The Application could be used in every country, so if I implement this abstraction, it is not difficult to offer more than these settings, but I only ever had to deal with these ones, so I have no ideas for more.

Does someone know a good example with good/useful formats?

Is there a resource to get all timezones in the world? I mean that what you allways see with

Code:
CET -  GMT +1 Berlin
CET -  GMT +1 Amsterdam
.....


something like that.

Thank you very much!


kind regards,
Andreas


Last edited by andreas on Wed May 05, 2010 10:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Wed Nov 12, 2003 1:37 am    Post subject: Reply with quote

You don't need to reinvent the wheel. PHP supports locale settings like any UNIX application (I believe it should work on windows too).

You have functions for handling currency formatting, date formatting and translations (through gettext), and your OS already comes with database for basically any country/language.

We just finished uploading a site that uses all of those functions to provide full i18n and l10n support yesterday. Right now we only use Hebrew and English, but adding new languages or currencies should be trivial.

Look into the following PHP functions:
- setlocale() - to define the locale settings
- money_format() - to format a money string according to the locale settings
- date() - format a date according to the locale settings
- gettext() - provide translations through the gettext library

I also wrote a plugin wrapper for gettext inside smarty, including support for variables and plural forms, as well as a string scanner script. I posted an old version of that plugin few months ago in the Tips & Tricks section but I'll do my best to post the latest version tomorrow.
Back to top
View user's profile Send private message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed Nov 12, 2003 10:20 am    Post subject: Reply with quote

Hi!

money_format()? I can't belive... thats PHP Wink

But what means "l10n"? localisation?

OK, but I don't know if it is flexible enough for me. So for example I want everybody to use the same currency, but his own(local) number-format. So someone from USA should read USD 123,123.99, and someone from Germany should read 123.123,99 EUR.

For me it seems to be better to use something like:

Code:
function something($number)
{
    $locale = localeconv();
    return number_format($number,
       $locale['frac_digits'],
       $locale['decimal_point'],
       $locale['thousands_sep']);
}


Or what do you think? OK, I can supress the currency-symbol, but would you use this instead?

Next problem is, that the number of decimales can be different, which is stored in database. So I use a wrapper for number_format to set these values and pass the return-value to smarty.

net is, that users have a form to fill a text-field which should contain a price. So I first of all have to validate it, if ist fits to the users local settings, and after this I have to convert it to a real float-number, as 123123.99, so PHP can calculate with it, and that's the way these prices are written into database. I don't know how setlocale and could help here.

Next point is "date". I sometimes have only dates, and sometimes have date+time. So I think about writing 2 smarty-functions, one called "date" and one called "datetime", wich are used as {$timestamp|datetime}

But I am not happy with this, because I have to get the users settings from another Object(global $usersettings->getTimeformat()) , but I think this is the best way.

I don't feal good to use setlocale with date/time, because - as far as I know - It would change the timezone, and that's not what I want to do. So if I use a local timezone, I _must_ display the servertime also, because this time is very important in my application.

And gettext, I don't know it would help, because I wand to implement multilanguage support into smarty, as is described in tips & tricks here. But I'm not shure how to implement it into my PHP-Scripts, because some messages come from the PHP-Script, which are not hard coded into the templates. So same status-messages which are stored in the database, I planed to use some Language-Files for this and only use an own function which gets an ID f?r the status-message and returns the String in correct language.

But thank you very much fpr your thoughts, I will be happy to take a look into your implementation!

regards,
Andreas

________
SH150i

Last edited by andreas on Fri Feb 04, 2011 9:01 am; edited 2 times in total
Back to top
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Wed Nov 12, 2003 10:42 am    Post subject: Reply with quote

Quote:
But what means "l10n"? localisation?


Yes, localization.

Quote:
OK, but I don't know if it is flexible enough for me. So for example I want everybody to use the same currency, but his own(local) number-format. So someone from USA should read USD 123,123.99, and someone from Germany should read 123.123,99 EUR.


The locales database sets all of those fields - the currency symbol, position, tousands seperator, grouping, and much more. You don't need to let the user enter his own format. You just let the user choose his locale - en_US for US english, he_IL for hebrew israel, etc - and it takes care of everything for you.

Quote:

net is, that users have a form to fill a text-field which should contain a price. So I first of all have to validate it, if ist fits to the users local settings, and after this I have to convert it to a real float-number, as 123123.99, so PHP can calculate with it, and that's the way these prices are written into database. I don't know how setlocale and could help here.


I don't understand. Are you saying that in the update price form the user enters "$99.9" and not 99.9? I'm not sure that it is a good practice, I think they should enter an unformatted number. But I'm pretty sure the locale can do that conversion too.

Quote:

Next point is "date". I sometimes have only dates, and sometimes have date+time. So I think about writing 2 smarty-functions, one called "date" and one called "datetime", wich are used as {$timestamp|datetime}

But I am not happy with this, because I have to get the users settings from another Object(global $usersettings->getTimeformat()) , but I think this is the best way.

I don't feal good to use setlocale with date/time, because - as far as I know - It would change the timezone, and that's not what I want to do. So if I use a local timezone, I _must_ display the servertime also, because this time is very important in my application.


Please see: http://il.php.net/strftime
For example, strftime('%x') will format the date according to the current locale, '%X' will format the time. Smarty's date_format modifier uses this function.

Quote:

And gettext, I don't know it would help, because I wand to implement multilanguage support into smarty, as is described in tips & tricks here. But I'm not shure how to implement it into my PHP-Scripts, because some messages come from the PHP-Script, which are not hard coded into the templates. So same status-messages which are stored in the database, I planed to use some Language-Files for this and only use an own function which gets an ID für the status-message and returns the String in correct language.


I don't like the method described in the tips&tricks section that uses XML file for the translation. I've seen it before writing our site, but I don't think there is a point in using such system when you have gettext support in PHP.

Gettext is already very popular and there are many UNIX programs which use it. There are programs like poEdit that lets you edit the catalog in a GUI interface, making it easier for the translators.

Gettext translate strings, not string ID's. For example, you could use this inside your PHP application:
echo _('Welcome to my site');

(_() is an alias to gettext())

And it will look for this string in the current locale catalog. If it won't find a match, it will display it in english.

The smarty plugin I wrote is just a wrapper for those functions, it lets you use them inside the templates like: {t}Welcome to my site{/t}.

As I said I will try to write a small tutorial about smarty & gettext soon.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP