I need some way to retrieve the language of a Calendar object, in order to e.g. encode it. I would like to add a constant for each cFOO class in Calendar.pmod/Language.pmod.
My wanted change in Calendar.pmod/Language.pmod looks like the following (shortened here since there are many small changes):
class cISO { constant name = "eng"; class cSWEDISH { constant name = "swe"; [...] class cLATVIAN { constant name ="lav"; class cLATVIAN_UNICODE { constant name ="lav"; class cSERBIAN { constant name ="srp"; class cSERBIAN_UNICODE { constant name ="srp";
So the following code will retrieve the ISO 639-2 string of the language the Calendar object is using the new property of the Language object:
The default language will be "eng" on most systems.
Calendar.Second()->language()->name;
(4) Result: "eng"
Since I do not have cvs access, I can supply a diff file, it is done against latest 7.7 source snapshot.
What do you guys say?
sounds interresting. but i am not yet quite sure why youy need this. could you elaborate with a usage example?
and i'd like to see the diff. can you upload it somewhere?
(or send it here if it is not to large)
greetings, martin.
I develop Roxen modules and since I sometimes needs to handle the _encode and _decode process for my objects since that is not handled automatically for me.
class MyEntityClass(Calendar.Minute min) { inherit RXML.Scope; constant is_RXML_encodable = 1;
array(int|string) _encode() { array(int|string) a = ({ time->unix_time(), min->language()->name }); // ---------- here it is used -----------------^ return a; }
void _decode (array(int|string) a) { [int t, string lang] = a; time = Calendar.set_language(lang)->Second ("unix", t); cal = 0; }
`[](string var, void|RXML.Context ctx, void|RXML.Type want_type) { [...] } }
Otherwise I have to look at what _sprintf tells me the object name is which i.e. swedish would be cSWE.
I hope this is not too much text:
--- Language.pmod 2006-08-07 18:52:53.000000000 +0200 +++ pike/pike-src/Pike-v7.7-snapshot/lib/modules/Calendar.pmod/Language.pmod 2006-08-07 18:55:12.000000000 +0200 @@ -503,6 +503,7 @@ { inherit _ymd_base;
+ constant name = "eng"; constant month_names= ({"January","February","March","April","May","June","July","August", "September","October","November","December"}); @@ -544,6 +545,8 @@ { inherit _ymd_base;
+ constant name = "swe"; + static private constant month_names= ({"januari","februari","mars","april","maj","juni","juli","augusti", "september","oktober","november","december"}); @@ -642,6 +645,7 @@ { inherit _ymd_base;
+ constant name = "de_at"; static private constant month_names= ({"jänner","feber","märz","april","mai","juni","juli","august", "september","oktober","november","dezember"}); @@ -659,12 +663,14 @@ // Welsh // source: book
+constant cWEL=cWELSH; constant cCY=cWELSH; constant cCYM=cWELSH; class cWELSH { inherit _ymd_base;
+ constant name = "cym"; static private constant month_names= ({"ionawr","chwefror","mawrth","ebrill","mai","mehefin", "gorffenaf","awst","medi","hydref","tachwedd","rhagfyr"}); @@ -698,6 +704,8 @@ { inherit _ymd_base;
+ constant name = "spa"; + static private constant month_names= ({"enero","febrero","marzo","abril","mayo","junio", "julio","agosto","setiembre","octubre","noviembre","diciembre"}); @@ -745,6 +753,8 @@ { inherit _ymd_base;
+ constant name ="por"; + static private constant month_names= ({ "Janeiro", @@ -804,6 +814,8 @@ { inherit _ymd_base;
+ constant name = "hun"; + static private constant month_names= ({"Január","Február","Március","Április","Május","Június", "Július","August","September","October","November","December"}); @@ -841,6 +853,8 @@ { inherit _ymd_base;
+ constant name = "lat"; + static array(string) month_names= ({"Ianuarius", "Februarius", "Martius", "Aprilis", "Maius", "Iunius", "Iulius", "Augustus", "September", "October", "November", "December" }); @@ -920,6 +934,8 @@ { inherit _ymd_base;
+ constant name = "kal"; + static private constant month_names= ({ "januari", @@ -953,12 +969,15 @@
// source: anonymous unix locale file
+constant cICE=cICELANDIC; constant cIS=cICELANDIC; // Icelandic constant cISL=cICELANDIC; class cICELANDIC { inherit _ymd_base;
+ constant name = "isl"; + static private constant month_names= ({ "Januar", @@ -992,12 +1011,15 @@
// source: anonymous unix locale file
+constant cPER=cPERSIAN; constant cFA=cPERSIAN; // Persian (Iran) constant cFAS=cPERSIAN; class cPERSIAN { inherit _ymd_base;
+ constant name ="fas"; + static private constant month_names= ({ "zanwyh", // <zj><a+><n+><w+><yf><h+> @@ -1037,6 +1059,8 @@ { inherit _ymd_base;
+ constant name ="afr"; + static private constant month_names= ({ "Januarie", @@ -1076,6 +1100,8 @@ { inherit _ymd_base;
+ constant name ="gle"; + static private constant month_names= ({ "Eanáir", @@ -1120,11 +1146,14 @@ // source: anonymous unix locale file
constant cEU=cBASQUE; // Basque (Spain) +constant cBAQ=cBASQUE; constant cEUS=cBASQUE; class cBASQUE { inherit _ymd_base;
+ constant name ="eus"; + static private constant month_names= ({ "urtarrila", @@ -1164,6 +1193,8 @@ { inherit _ymd_base;
+ constant name ="nor"; + static private constant month_names= ({ "januar", @@ -1205,6 +1236,8 @@ { inherit _ymd_base;
+ constant name ="nld"; + static private constant month_names= ({ "januari", @@ -1244,6 +1277,8 @@ { inherit _ymd_base;
+ constant name ="pol"; + static private constant month_names= ({ "styczen", // <s><t><y><c><z><e><n'> @@ -1278,6 +1313,8 @@ { inherit _ymd_base;
+ constant name ="pol"; + static private constant month_names= ({ "stycze\x0143", // <s><t><y><c><z><e><n'> @@ -1317,6 +1354,8 @@ { inherit _ymd_base;
+ constant name ="tur"; + static private constant month_names= ({ "Ocak", @@ -1353,6 +1392,8 @@ { inherit _ymd_base;
+ constant name ="tur"; + static private constant month_names= ({ "Ocak", @@ -1393,6 +1434,8 @@ { inherit _ymd_base;
+ constant name ="deu"; + static private constant month_names= ({ "Januar", @@ -1432,6 +1475,8 @@ { inherit _ymd_base;
+ constant name ="lav"; + static private constant month_names= ({ "janvaris", // <j><a><n><v><a-><r><i><s> @@ -1468,6 +1513,8 @@ { inherit _ymd_base;
+ constant name ="lav"; + static private constant month_names= ({ "janv\x0101""ris", // <j><a><n><v><a-><r><i><s> @@ -1507,6 +1554,8 @@ { inherit _ymd_base;
+ constant name ="fin"; + static private constant month_names= ({ "tammikuu", @@ -1546,6 +1595,8 @@ { inherit _ymd_base;
+ constant name ="lit"; + static private constant month_names= ({ "sausio", @@ -1580,6 +1631,8 @@ { inherit _ymd_base;
+ constant name ="lit"; + static private constant month_names= ({ "sausio", @@ -1619,6 +1672,8 @@ { inherit _ymd_base;
+ constant name ="est"; + static constant month_names= ({ "jaanuar", @@ -1655,6 +1710,8 @@ { inherit _ymd_base;
+ constant name ="glg"; + static private constant month_names= ({ "Xaneiro", @@ -1685,11 +1742,14 @@ void create() { SETUPSTUFF; } }
+constant cIND=cINDONESIAN; constant cID=cINDONESIAN; class cINDONESIAN { inherit _ymd_base;
+ constant name ="ind"; + static private constant month_names= ({ "Januari", @@ -1726,6 +1786,8 @@ { inherit _ymd_base;
+ constant name ="fra"; + static private constant month_names= ({ "janvier", @@ -1762,6 +1824,8 @@ { inherit _ymd_base;
+ constant name ="ita"; + static private constant month_names= ({ "gennaio", @@ -1798,6 +1862,8 @@ { inherit _ymd_base;
+ constant name ="cat"; + static private constant month_names= ({ "gener", @@ -1834,6 +1900,8 @@ { inherit _ymd_base;
+ constant name ="slv"; + static constant month_names= ({ "januar", @@ -1868,6 +1936,8 @@ { inherit cSLOVENIAN;
+ constant name ="slv"; + static constant week_day_names= ({ "ponedeljek", @@ -1888,6 +1958,8 @@ { inherit _ymd_base;
+ constant name ="fao"; + static private constant month_names= ({ "januar", @@ -1918,12 +1990,15 @@ void create() { SETUPSTUFF; } }
+constant cRUM=cROMANIAN; constant cRO=cROMANIAN; // Romanian constant cRON=cROMANIAN; class cROMANIAN { inherit _ymd_base;
+ constant name ="ron"; + static constant month_names= ({ "Ianuarie", @@ -1958,6 +2033,8 @@ { inherit cROMANIAN;
+ constant name ="ron"; + static constant week_day_names= ({ "Luni", @@ -1972,12 +2049,15 @@ void create() { SETUPSTUFF; } }
+constant cSCR=cCROATIAN; constant cHR=cCROATIAN; // Croatian constant cHRV=cCROATIAN; // Croatian class cCROATIAN { inherit _ymd_base;
+ constant name ="hrv"; + static private constant month_names= ({ "Sijecanj", // <S><i><j><e><c<><a><n><j> @@ -2012,6 +2092,8 @@ { inherit _ymd_base;
+ constant name ="hrv"; + static private constant month_names= ({ "Sije\415anj", @@ -2048,6 +2130,8 @@ { inherit _ymd_base;
+ constant name ="dan"; + static private constant month_names= ({ "januar", @@ -2103,12 +2187,15 @@ void create() { SETUPSTUFF; } }
+constant cSCC=cSERBIAN; constant cSR=cSERBIAN; // Serbian (Yugoslavia) constant cSRP=cSERBIAN; class cSERBIAN { inherit _ymd_base;
+ constant name ="srp"; + static constant month_names= ({ "januar", @@ -2144,6 +2231,8 @@ { inherit cSERBIAN;
+ constant name ="srp"; + static constant week_day_names= ({ "ponedeljak",
_encode and _decode cannot handle Calendar objects more specifically. I can not make it general in the sense that the object should not need to know before hand what language the calendar object has. That should be simple to retrieve by the object itself.
That was a bit unclear, I apologize.
_encode() and _decode() are method used when the cache system in roxen byte compile, in my case, RXML-variables. My class need to do the _encode() and _decode() itself since the Calendar object needs to be decoded to a simpler data type, in my example an array, and from the same array encoded back to the Calendar object in _encode(). So to _decode() the Calendar object I need to know the language in that so I can recreate the Calendar object, to whatever language is set in it, correctly.
In that case, wouldn't it be nicer to have _encode and _decode methods in the Calendar objects directly?
Not that I oppose having the languages accessible this way; that could probably be useful for other things.
I totally agree. Both would be perfect! I've had different reasons to retrieve the language of the Calendar object but that was a while ago and I opted for a uglier solution, don't remember what it was, though.
Adding _encode() and _decode() is beyond my skills. Especially since the Calendar module is such a complex module.
with innermost, do you mean TimeRange?
does someone have a useful example for _encode and _decode?
should they be documented here: http://pike.ida.liu.se/generated/manual/modref/ex/lfun_3A_3A.html or is that a different category?
greetings, martin.
should they be documented here: http://pike.ida.liu.se/generated/manual/modref/ex/lfun_3A_3A.html
Yes, they probably should. There's also some doc for the Codec class in the master but it's far from complete.
i just wanted to ask about that, maybe it is better to use the unix/posix? way to specify locales.
they should cover all the calendar variations we might end up with (and i don't thinkt that the ISO 639-2 table has any entries that are not matched by a locale name)
greetings, martin.
Maybe, I do not have an opinion on posix/unix names vs ISO 693-2. I thought that ISO 639-2 would be more natural since it was defined in there, except for de_at, which is not part of that standard.
Is there an possibility that my proposed changes could could be added to Language.pmod?
i have not seen any counter arguments, so yes, the possibility is there.
unless someone beats me to it i may get to that in a few days,
though i am more interrested in the _encode/_decode functions, but i have no idea yet what needs to be done to get those to work.
greetings, martin.
Basically _encode nees to take the entire object state and serialize it into something using only the basic data types (i.e. no objects, programs, funtions or types). _decode needs to decode this serialized format and set the object state accoringly.
class A (string foo, int bar) { array _encode() { return ({ foo, bar }); }
array _decode(array in) { [ foo, bar ] = in; } }
void main() { A a=A("hello",18); write("%O\n", encode_value(a)); write("%O\n", decode_value(encode_value(a))->foo); }
ahh, so the trick is really, that it does not matter how the return value in _encode looks like, as long as _decode knows how to interpret it?
that's what encode() should be doing after it gets passed an some encodable values.
eg. for the calendar, the basic information needed should be fairly minimal (timestamps for the ranges, timezone, language. all other values should be calculated from those)
greetings, martin.
Note also that you are free to use non-basic data types, as long as they can be encoded:d in turn.
Probably to be able to instantiate those languages given their 639-2 language code with the Calendar module too.
pike-devel@lists.lysator.liu.se