php

You are currently browsing articles tagged php.

Navelfluff är en trespråkig blogg. Ibland häller jag ur mej på svenska, ibland på finska och ibland på engelska, lite beroende på vad jag har att säja och till vilken publik jag riktar mej. Språkindelningen annoterar jag med Wordpressens kategorier, vilka jag oxå använder för kategorisering av själva innehållet (geek, Timor-Leste, pynja, osv). Det är en debattfråga huruvida det kan tänkas syntaktiskt korrekt att använda samma slags metadata att annotera både innehållet och språket för inlägget men jag geek och inte lingvist, så jag godkänner mitt beteenede i detta fall.

Wordpress å andra sidan är inte mångspråkig, vilket kanske inte är så konstigt med tanke på att de flesta bloggar skrivs på ett och samma språk. Visst, man kan få WP att tala ett annat språk, men det är fortfarande bara tal om ett annat språk i taget. Vilket jag tyckte var lite trist.

Ända tills lite tidigare idag tyckte nämligen Navelfluff att “please leave a comment” för alla inlägg, oavsett vilket språk inlägget var skrivet på. Och i samband med en kommentar jag fick angående Tarski-temats lite lakoniska “no comments”-statustext i anslutning till inlägg som saknar kommentarer (till skillnad från inlägg som inte önskar kommentarer) så tog jag och gjorde nåt åt saken.

Följande plugin lokaliserar skriv-en-kommentar- eller inga-kommentarer -texten. Pluggaren baserar sej på change_no_comments_text -pluginnen av Tony Trainor. Förklaring följer efter koden.

function change_comments_text($text, $number) {
    global $post;
    $lang = "any" ;

    $cats = wp_get_post_categories( $post->ID );
    $stac = array_flip( $cats );

    if( array_key_exists( 3, $stac )) $lang = "sv";
    if( array_key_exists( 4, $stac )) $lang = "fi";

    $text = Array( "any" => array( 0 => "Please leave a comment! ",
                                  1 => "1 comment ",
                                  2 => "$number comments "),
                  "sv" => array(  0 => "Skriv en kommentar! ",
                                  1 => "1 kommentar ",
                                  2 => "$number kommentarer "),
                  "fi" => array(  0 => "Anna palautetta! ",
                                  1 => "1 palaute ",
                                  2 => "$number palautetta "));
    $num = $number;
    if( $num > 2 ) $num = 2;
    $ret = $text[$lang][$num];

    return $ret;
}

add_filter('comments_number', 'change_comments_text', 10, 2);

Varje postning har ett antal kategorier (noll eller flera) och de representeras som en array vars värden utgör kategori-ID:na. Efter en del reverse-engineering kategori-arrayn (att de facto läsa dokumentationen är tråkigt :) ) insåg jag att mina svenskspråkiga inlägg har en kategori-ID 3 och mina finskspråkiga ID 4. [0] 

I koden används ett lite fult trick, nämligen array_flip samt array_key_exists för att kolla om artikeln har kategori-ID tre eller fyra. Samma sak skulle ja antagligen skrivit elegantare med array_search men av nån orsak fick jag inte den funktionen att funktionera. Men så är jag oxå nybörjare, så jag har en bortförklaring.

Slutligen konstateras att jag har ingen aning vad parametrarna 10 och 2 gör i slutet av alltsammans.

Har du en flerspråkig blogg (hej skrubu! hej nikc!) och språk-kategoriserar dina inlägg så behöver du “bara” gissa fram vilka kategori-ID:n du använder (vink: ersätt return $ret; i koden ovan med return "$ret <!-- $lang $num " . implode( "|", array_values( $cats )) . " -->";), infoga ett kommentarblock i början av koden med de av WP obligerade kommentarfälten, inslut kodsnutten i <?php ... ?>-markering, och slutligen spara hela rasket i ditt wp-content/plugins -direktorat. 

Skulle det mot all vettig förmodan finnas en social beställning på denna pluggare i ett människovänligare format, så … vänligen skriv en kommentar!

[0] Övriga (samt o-språk-kategoriseade) inlägg behandlas som om de vore engelska och de med flera språk-kategoriseringar får den sista lokaliseringen i listan. Det är så sällan jag skriver ett inlägg med flera såpråk att jag inte orkar koda om pluggaren så att den lokaliserar till alla noterade språk.

Tags: , , ,

Note so self: Regardless how small a project may seem, the way to make a one day job a two week mess is to do first and plan later. It’s been said many times before, but obviously it needs to be said again.  First plan, then do.

I had this small project (actually more of “just a job”) on the job to create a way for our employees to enrol to our “Little Christmas” party.  The way i attacked the problem was first to think of all kinds of wild, “practical” and “convenient” ways to get it done.  Then i ignored it for a week.  Then came the deadline and the “practical” and “convenient” ways turned out not to be.  All sorts of “solutions” came and were recycled.  Frustration ensued.
Last night, in some angst, i sat coding and re-coding what actually seemed to be a pretty good solution: a set of connected PHP forms on a web site of mine, backed with a mySQL database.  Yes, i know it’s hugely overkill, but i couldn’t get SQLite to work anywhere (but my home box) so i moved to a bigger hammer.  Still, the code was full of bugs.  Logical bugs mainly, because things were hugely complex and Just Wouldn’t Work.  At least not in a logical way.

So i decided to Plan First.

At bedtime, in bed, i scribbled in painstaking detail exactly what would happen with my form in all its states, removing all needless complexity, on my PDA.  And voilá, it really wasn’t that hard!  All the complexity was there just because i hadn’t properly thought of the problem before i started working on it.  And the deeper i’d got into my messy code, the more complex and filled with special cases it got.

So this morning, i got up one hour before normal wake-up time, sat down in the coding sofa with my laptop, and made it work.

OK, granted, there were still some minor bugs left when i arrived at work and had to demo the thing, but even they were easy to understand and stomp out.
Moral: When you understand the problem, then creating a solution isn’t really that hard to do. I hope i can remember that next time i attempt to “just do a small job”.

Tags: , , ,

Ooh! I’m a geek! I have created my first -enabled web page :) . My script shows the next event on one of the courses i’m an assistant on. My humble snippet reads from a flat, semicolon-delimited text file (i could snob away and say it’s a CSV file), parses the date from the first field, compares that to today’s date and if it’s for the next/current event, shows the event with the date prettified to local standards. The nifty features are that it finds the data file even if the web page is read through a symlink, and that no event information is shown if the course is already over.

Tags: , ,

Phew. I have access to the old navelfluff server again, which means that my site will soon return to normality. There is but one problem. I have grown to like Wordpress, but my old blog is a nice mixture of and , which i would feel i’d betray if i left them for somebody newer and shinier… Oh geekness. Tcl is nice and so is Tclhttpd. I am able to do all kinds of niftyness on my server and it works great on the scale i am on. Nanoblogger is diggable because it’s basically just a shell script — you create your blog entries over an ssh connection. It’s geeky. It feels at home, close to the metal. But. Wordpress is productive. The treshold for publishing is lower. I get more posted. But it’s also in , and i don’t hack php. It doesn’t feel hackish. Maybe i just have to look at .

Tags: , , , , ,

Bad Behavior has blocked 761 access attempts in the last 7 days.