* [Caml-list] Printf and i18n @ 2002-04-17 17:37 Jérôme Marant 2002-04-17 17:53 ` [Caml-list] Modular subject-observer pattern ? Pascal Grossé 2002-04-18 6:47 ` [Caml-list] Printf and i18n Daniel de Rauglaudre 0 siblings, 2 replies; 11+ messages in thread From: Jérôme Marant @ 2002-04-17 17:37 UTC (permalink / raw) To: caml-list Hi, I was thinking about writing a small gettext-like module for handling internationalisation of strings in OCaml programs. But it is quite useless if it cannot work with printf: the printf format parameter looks like a string but it isn't (printf is using some internal magics). Any idea about how this could be handled? Thanks. Regards, -- Jérôme Marant <jerome@marant.org> <jerome.marant@free.fr> http://marant.org ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* [Caml-list] Modular subject-observer pattern ? 2002-04-17 17:37 [Caml-list] Printf and i18n Jérôme Marant @ 2002-04-17 17:53 ` Pascal Grossé 2002-04-18 6:47 ` [Caml-list] Printf and i18n Daniel de Rauglaudre 1 sibling, 0 replies; 11+ messages in thread From: Pascal Grossé @ 2002-04-17 17:53 UTC (permalink / raw) To: caml-list Hi, I'm read how to implement the subject-observer pattern using objects, as shown on: http://pauillac.inria.fr/~remy/cours/appsem/ocaml-objects.html#toc13 I have a question : is it possible to have a simple version of this pattern without using objects, i.e. with modules/functors ? Thanks, Pascal Grosse ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-17 17:37 [Caml-list] Printf and i18n Jérôme Marant 2002-04-17 17:53 ` [Caml-list] Modular subject-observer pattern ? Pascal Grossé @ 2002-04-18 6:47 ` Daniel de Rauglaudre 2002-04-18 8:01 ` John Prevost 2002-04-18 8:02 ` Jérôme Marant 1 sibling, 2 replies; 11+ messages in thread From: Daniel de Rauglaudre @ 2002-04-18 6:47 UTC (permalink / raw) To: caml-list Hi, On Wed, Apr 17, 2002 at 07:37:19PM +0200, Jérôme Marant wrote: > I was thinking about writing a small gettext-like module > for handling internationalisation of strings in OCaml programs. > > But it is quite useless if it cannot work with printf: the > printf format parameter looks like a string but it isn't > (printf is using some internal magics). In my program GeneWeb, I use internationalization with printf. I have a function ftransl function defined like this (revised syntax): value ftransl lang (fmt : format 'a 'b 'c) : format 'a 'b 'c = valid_format fmt (transl lang (Obj.magic fmt : string)); The function "transl", called inside this function, takes the language and the format fmt constrained to be a string and applies the translation. The function "valid_format" checks that this resulting translation has the same % defined in the same order than fmt. Return the translated format if ok. If not ok, it returns the initial format, but you can fail, if you prefer. Therefore, instead of printf "hello, my name is %s\n" name you can write: printf (ftransl "hello, y name is %s\n") name For the translations, I use a lexicon file, containing a list of entries which I want to be i18n. It is an idea I took from Caml Light. The first string, without language, is the format to be translated, in a vehicular language (actually English), and I give the translation in each language I want. E.g. an entry can be: hello, my name is %s\n en: hello, my name is %s\n es: buenos dias, me llamo %s\n fr: bonjour, je m'appelle %s\n -- Daniel de RAUGLAUDRE daniel.de_rauglaudre@inria.fr http://cristal.inria.fr/~ddr/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 6:47 ` [Caml-list] Printf and i18n Daniel de Rauglaudre @ 2002-04-18 8:01 ` John Prevost 2002-04-18 8:02 ` Jérôme Marant 1 sibling, 0 replies; 11+ messages in thread From: John Prevost @ 2002-04-18 8:01 UTC (permalink / raw) To: Daniel de Rauglaudre; +Cc: caml-list >>>>> "dr" == Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr> writes: dr> For the translations, I use a lexicon file, containing a list dr> of entries which I want to be i18n. It is an idea I took from dr> Caml Light. The first string, without language, is the format dr> to be translated, in a vehicular language (actually English), dr> and I give the translation in each language I want. E.g. an dr> entry can be: dr> hello, my name is %s\n dr> en: hello, my name is %s\n dr> es: buenos dias, me llamo %s\n dr> fr: bonjour, je m'appelle %s\n A good try, but unfortunately it doesn't allow for the more significant problem, which is word order variation. Imagine a simple format like: Printf.printf "%s's %s" "John" "document" The problem here is that we have the following translations (sort of, no character sets here): en: X's Y ja: X no Y ru: Y u X And of course, with more arguments you get more variety. One solution is to treat your formats as functions, rather than formats. This cuts down on the utility somewhat, since you have to sprintf instead of printing directly, but: let owns = [En, fun owner owned -> Printf.sprintf "%s's %s" owner owned; Ja, fun owner owned -> Printf.sprintf "%s no %s" owner owned; Ru, fun owner owned -> Printf.sprintf "%s u %s" owned owner] sort of gets you moving in the right direction. Of course, you need an automated system to handle things reasonably. This is why many modern printf-style systems, especially those for handling internationalization, include numbered or named slots to be filled in. For example, in Python: fmt = "%(owner)s's %(owned)s" fmt = "%(owner)s no %(owned)s" fmt = "%(owned) u %(owner)" can all be used as: fmt % {'owner':'John', 'owned':'document'} In Java: fmt = new MessageFormat("{0}''s {1}"); fmt = new MessageFormat("{0} no {1}"); fmt = new MessageFormat("{1} u {0}"); can all be used as: fmt.format(new Object[] {"John", "document"}) Either of these mechanisms allow reading a format from a catalog and using it in that way. The only way I can see doing this with something like O'Caml's current mechanism is introducing a new format function like printf which takes a format like: ("%s%s:{0}'s {1}" : ('a,'b,'c) format) ("%s%s:{0} no {1}" : ('a,'b,'c) format) ("%s%s:{1} u {0}" : ('a,'b,'c) format) Use the % model up front to indicate the argument types in argument order, then indicate what order the arguments are to be used separately. This is, of course, rather ugly. A richer format language would be preferable. If keyword arguments can't be made to work right when included in the type (as seems likely), perhaps a format language like Python's, but using numbered argument positions instead of names, so: ("%s %d" : (string -> int -> 'a,'b,'a) format) but also ("%(1)d %(0)s" : (string -> int -> 'a, 'b, 'a) format) John. ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 6:47 ` [Caml-list] Printf and i18n Daniel de Rauglaudre 2002-04-18 8:01 ` John Prevost @ 2002-04-18 8:02 ` Jérôme Marant 2002-04-18 8:13 ` Jacques Garrigue 2002-04-18 8:27 ` Daniel de Rauglaudre 1 sibling, 2 replies; 11+ messages in thread From: Jérôme Marant @ 2002-04-18 8:02 UTC (permalink / raw) To: caml-list On Thu, Apr 18, 2002 at 08:47:53AM +0200, Daniel de Rauglaudre wrote: > > In my program GeneWeb, I use internationalization with printf. I have > a function ftransl function defined like this (revised syntax): > > value ftransl lang (fmt : format 'a 'b 'c) : format 'a 'b 'c = > valid_format fmt (transl lang (Obj.magic fmt : string)); > > The function "transl", called inside this function, takes the language > and the format fmt constrained to be a string and applies the translation. > > The function "valid_format" checks that this resulting translation > has the same % defined in the same order than fmt. Return the translated > format if ok. If not ok, it returns the initial format, but you can > fail, if you prefer. > > Therefore, instead of > printf "hello, my name is %s\n" name > you can write: > printf (ftransl "hello, y name is %s\n") name Thanks. That sounds what I'm looking for. By the way, in order to improve the localization process, we would need to add the "argument reordering" feature to printf functions. For instance, with: printf " %2$d %1$s" "foo" 1 you get : 1 foo because some languages do not order words the same way. One more question : could you (or any other OCaml developer) give some explainations about the Obj module since, even "not for the casual user", it can be usefull sometimes (well at least for you and in the printf functions)? Thanks in advance. -- Jérôme Marant ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 8:02 ` Jérôme Marant @ 2002-04-18 8:13 ` Jacques Garrigue 2002-04-18 8:17 ` Chris Hecker 2002-04-18 8:21 ` Jérôme Marant 2002-04-18 8:27 ` Daniel de Rauglaudre 1 sibling, 2 replies; 11+ messages in thread From: Jacques Garrigue @ 2002-04-18 8:13 UTC (permalink / raw) To: jerome.marant; +Cc: caml-list > One more question : could you (or any other OCaml developer) give some > explainations about the Obj module since, even "not for the casual user", > it can be usefull sometimes (well at least for you and in the printf > functions)? The Obj module lets you access the internal representation of ocaml objects, as desribed in the "Interfacing with C" section of the manual. Once you have understood the details of this representation, you can start using it sparingly, to avoid having to write external C functions. i.e. it is best seen as an alternative to writing C code, and is not much safer than that (just a bit easier at the GC level). Jacques ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 8:13 ` Jacques Garrigue @ 2002-04-18 8:17 ` Chris Hecker 2002-04-18 8:21 ` Jérôme Marant 1 sibling, 0 replies; 11+ messages in thread From: Chris Hecker @ 2002-04-18 8:17 UTC (permalink / raw) To: Jacques Garrigue, jerome.marant; +Cc: caml-list >The Obj module lets you access the internal representation of ocaml >objects, as desribed in the "Interfacing with C" section of the >manual. It might be useful to explain the printf format string hackery in the manual. It's the only "legitimate" use of Obj.magic; you have to use it if you want to do varargs style functions like printf and parse the strings. Or, is there another use of it in the standard library that I'm missing? Chris ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 8:13 ` Jacques Garrigue 2002-04-18 8:17 ` Chris Hecker @ 2002-04-18 8:21 ` Jérôme Marant 1 sibling, 0 replies; 11+ messages in thread From: Jérôme Marant @ 2002-04-18 8:21 UTC (permalink / raw) To: caml-list On Thu, Apr 18, 2002 at 05:13:38PM +0900, Jacques Garrigue wrote: > > One more question : could you (or any other OCaml developer) give some > > explainations about the Obj module since, even "not for the casual user", > > it can be usefull sometimes (well at least for you and in the printf > > functions)? > > The Obj module lets you access the internal representation of ocaml > objects, as desribed in the "Interfacing with C" section of the > manual. > Once you have understood the details of this representation, you can > start using it sparingly, to avoid having to write external C > functions. i.e. it is best seen as an alternative to writing C code, > and is not much safer than that (just a bit easier at the GC level). Alright. Thanks a lot. -- Jérôme Marant ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 8:02 ` Jérôme Marant 2002-04-18 8:13 ` Jacques Garrigue @ 2002-04-18 8:27 ` Daniel de Rauglaudre 2002-04-18 9:33 ` Jérôme Marant 1 sibling, 1 reply; 11+ messages in thread From: Daniel de Rauglaudre @ 2002-04-18 8:27 UTC (permalink / raw) To: caml-list Hi, On Thu, Apr 18, 2002 at 10:02:42AM +0200, Jérôme Marant wrote: > By the way, in order to improve the localization process, we would need > to add the "argument reordering" feature to printf functions. > For instance, with: > > printf " %2$d %1$s" "foo" 1 I have something like that, indeed. It is also John Prevost's remark. For that I use the same system of lexicon, but I don't use printf, but just print_string for this kind of phrases. I have specific functions to translate that sort of cases. The string is scanned by an automat to build the resulting string. I have as example in my lexicon: %1 of %2 cs: %1 :g:%2 de: %1 von :d:%2 en: %2's %1 fr: %1 d[e']%2 it: %1 d[i']%2 pl: %1 :g:%2 There is also some system for elision (before vowels), in French and in Italian. In French, to translate "Adam's car", you have "la voiture d'Adam", not "la voiture de Adam". And there is a system of declination in German and Polish. The :g: introduces the genitive case for the next phrase. And I have a system to indicate genitive cases for the other terms. Another case, the way dates are printed: (date) cs: 1. %m %y/%d. %m %y/%m %y/%y de: 1. %m %y/%d. %m %y/%m %y/%y en: 1 %m %y/%d %m %y/%m %y/%y fr: 1er %m %y/%d %m %y/%m %y/%y it: 1o %m %y/%d %m %y/%m %y/%y pl: 1 %m %y/%d %m %y/%m %y/%y %m = month, %y = year %d = day The slash separate the cases: first in the month other in the month date when there is only month and year date when there is only year Of course, in all these examples, printf does not work: you have to apply print_string, or printf "%s" -- Daniel de RAUGLAUDRE daniel.de_rauglaudre@inria.fr http://cristal.inria.fr/~ddr/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 8:27 ` Daniel de Rauglaudre @ 2002-04-18 9:33 ` Jérôme Marant 2002-04-18 10:40 ` Daniel de Rauglaudre 0 siblings, 1 reply; 11+ messages in thread From: Jérôme Marant @ 2002-04-18 9:33 UTC (permalink / raw) To: caml-list On Thu, Apr 18, 2002 at 10:27:34AM +0200, Daniel de Rauglaudre wrote: > Hi, > > On Thu, Apr 18, 2002 at 10:02:42AM +0200, Jérôme Marant wrote: > > > By the way, in order to improve the localization process, we would need > > to add the "argument reordering" feature to printf functions. > > For instance, with: > > > > printf " %2$d %1$s" "foo" 1 > > I have something like that, indeed. It is also John Prevost's remark. > For that I use the same system of lexicon, but I don't use printf, but > just print_string for this kind of phrases. So, if I understood correctly, the string parameters are nothing but strings, right? ... > Of course, in all these examples, printf does not work: you have to > apply print_string, or printf "%s" I'ld love to see the feature in printf though. Regards, -- Jérôme Marant ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Printf and i18n 2002-04-18 9:33 ` Jérôme Marant @ 2002-04-18 10:40 ` Daniel de Rauglaudre 0 siblings, 0 replies; 11+ messages in thread From: Daniel de Rauglaudre @ 2002-04-18 10:40 UTC (permalink / raw) To: caml-list Hi, On Thu, Apr 18, 2002 at 11:33:23AM +0200, Jérôme Marant wrote: > So, if I understood correctly, the string parameters are nothing but > strings, right? In this case, yes: I use my own automat instead of (the one of) printf. > > Of course, in all these examples, printf does not work: you have to > > apply print_string, or printf "%s" > > I'ld love to see the feature in printf though. Could be an idea, indeed. -- Daniel de RAUGLAUDRE daniel.de_rauglaudre@inria.fr http://cristal.inria.fr/~ddr/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2002-04-18 21:09 UTC | newest] Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2002-04-17 17:37 [Caml-list] Printf and i18n Jérôme Marant 2002-04-17 17:53 ` [Caml-list] Modular subject-observer pattern ? Pascal Grossé 2002-04-18 6:47 ` [Caml-list] Printf and i18n Daniel de Rauglaudre 2002-04-18 8:01 ` John Prevost 2002-04-18 8:02 ` Jérôme Marant 2002-04-18 8:13 ` Jacques Garrigue 2002-04-18 8:17 ` Chris Hecker 2002-04-18 8:21 ` Jérôme Marant 2002-04-18 8:27 ` Daniel de Rauglaudre 2002-04-18 9:33 ` Jérôme Marant 2002-04-18 10:40 ` Daniel de Rauglaudre
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox