Hi, I'm currently writing a small wrapper layer for gettext. My current getText template looks like this:
---------------------------------------------------- string getText()(string msgid, string domain = null, Category cat = Category.Messages) { auto cmsg = toStringz(msgid); auto nmsg = dcgettext(domain ? toStringz(domain) : null, cmsg, cat); if(cmsg == nmsg) return msgid; else { string nstr = cast(string)nmsg[0 .. strlen(nmsg)]; return nstr; } } string getText(string msgid, string domain = null, Category cat = Category.Messages)() { auto nmsg = dcgettext(domain ? domain.ptr : null, msgid.ptr, cat); if(msgid.ptr == nmsg) return msgid; else { string nstr = cast(string)nmsg[0 .. strlen(nmsg)]; return nstr; } } ---------------------------------------------------- As string literals in D are zero terminated, there's no need for the toStringz overhead. The overload taking compile time parameters takes advantage of that. The code works and can be used like this: ---------------------------------------------------- writeln(getText!"Hello World!"); //no toStringz writeln(getText("Hello World!")); //toStringz ---------------------------------------------------- But if somehow possible I'd like to merge the templates so that there is only one way to call getText and the fastest way is chosen automatically. Does anyone know how to do that? -- Johannes Pfau
signature.asc
Description: PGP signature