![]() |
|
Snippets |
|
If you are dealing with i18n and client side effects, you might need an equivalent for the format_number_choice() helper in Javascript. Here is a simplified version:
function format_number_choice(text_string, replace_hash, number) { pattern = new RegExp("\\["+number+"\\]\s?([^\|]*)"); function replace_in_string(text_string, replace_hash) { for(var i in replace_hash) text_string = text_string.replace(new RegExp(i), replace_hash[i]); return text_string; } matches=text_string.match(pattern); if(matches != null) return replace_in_string(matches[1], replace_hash); else { pattern = /\[else\]\s?([^\|]*)/; matches=text_string.match(pattern); if(matches != null) return replace_in_string(matches[1], replace_hash); else return "not found"; } }
Use it in your HTML code as follows:
<script language="JavaScript" type="text/javascript"> document.write(format_number_choice("[0] No apple |[1] One Apple |[2] Two Apples |[else] Many Apples", {'Apple' : 'fruit'}, 0)) document.write(format_number_choice("[0] No apple |[1] One Apple |[2] Two Apples |[else] Many Apples", [], 1)) document.write(format_number_choice("[0] No apple |[1] One Apple |[2] Two Apples |[else] Many Apples", [], 2)) document.write(format_number_choice("[0] No apple |[1] One Apple |[2] Two Apples |[else] Many Apples", [], 3)) </script>
This will output:
No fruit One Apple Two Apples Many Apples
Now, just use enclose the first argument with the __() helper, and you have an internationalized format_number_choice() on the client side.