Ignore:
Timestamp:
07/21/09 16:08:35 (3 years ago)
Author:
AntonioTerceiro
Message:

Item5692: new release of TopicTranslationsPlugin

  • New %CURRENTLANGUAGESUFFIX% macro.
  • Porting to Foswiki
  • Removed references to TWiki were applicable.
  • updated copyright notice
  • Adding a proper build system
  • Using regular regular expression delimiter in the SCRIPT_ENV test
Location:
trunk/TopicTranslationsPlugin/lib/Foswiki
Files:
2 added
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/TopicTranslationsPlugin/lib/Foswiki/Plugins/TopicTranslationsPlugin.pm

    r1340 r4517  
    11# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ 
    22# 
    3 # Copyright (C) 2005 Antonio S. de A. Terceiro, asaterceiro@inf.ufrgs.br 
     3# Copyright (C) 2005-2009 Antonio Terceiro, terceiro@softwarelivre.org 
    44# 
    55# This program is free software; you can redistribute it and/or 
     
    1515 
    1616# ========================= 
    17 package TWiki::Plugins::TopicTranslationsPlugin; 
     17package Foswiki::Plugins::TopicTranslationsPlugin; 
    1818 
    1919# ========================= 
     
    3030use I18N::AcceptLanguage; 
    3131 
    32 # This should always be $Rev$ so that TWiki can determine the checked-in 
     32# This should always be $Rev$ so that Foswiki can determine the checked-in 
    3333# status of the plugin. It is used by the build automation tools, so 
    3434# you should leave it alone. 
     
    4747 
    4848    # check for Plugins.pm versions 
    49     if( $TWiki::Plugins::VERSION < 1.024 ) { 
    50         TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" ); 
     49    if( $Foswiki::Plugins::VERSION < 1.024 ) { 
     50        Foswiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" ); 
    5151        return 0; 
    5252    } 
    5353 
    5454    # Get plugin debug flag 
    55     $debug = TWiki::Func::getPluginPreferencesFlag( "DEBUG" ); 
     55    $debug = Foswiki::Func::getPluginPreferencesFlag( "DEBUG" ); 
    5656 
    5757    # those should be preferably set in a per web basis. Defaults to the 
    5858    # corresponding plugin setting (or "en" if someone messes with it) 
    59     my $trans = TWiki::Func::getPreferencesValue("TOPICTRANSLATIONS") || TWiki::Func::getPluginPreferencesValue("TOPICTRANSLATIONS") || "en"; 
     59    my $trans = Foswiki::Func::getPreferencesValue("TOPICTRANSLATIONS") || Foswiki::Func::getPluginPreferencesValue("TOPICTRANSLATIONS") || "en"; 
    6060    @translations = split(/,\s*/,$trans); 
    61     $redirectMethod = TWiki::Func::getPreferencesValue("REDIRECTMETHOD") || TWiki::Func::getPluginPreferencesValue("REDIRECTMETHOD") || "http"; 
    62     $userLanguage = TWiki::Func::getPreferencesValue("LANGUAGE") || "en";     
     61    $redirectMethod = Foswiki::Func::getPreferencesValue("REDIRECTMETHOD") || Foswiki::Func::getPluginPreferencesValue("REDIRECTMETHOD") || "http"; 
     62    $userLanguage = Foswiki::Func::getPreferencesValue("LANGUAGE") || "en";     
    6363 
    6464    # first listed language is the default one: 
     
    7474 
    7575    # must I redirect to the best available translation? 
    76     my $mustRedirect = (! TWiki::Func::getPluginPreferencesFlag("DISABLE_AUTOMATIC_REDIRECTION")); 
     76    my $mustRedirect = (! Foswiki::Func::getPluginPreferencesFlag("DISABLE_AUTOMATIC_REDIRECTION")); 
    7777    checkRedirection() if $mustRedirect; 
    7878 
    7979    # Plugin correctly initialized 
    80     TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $debug; 
     80    Foswiki::Func::writeDebug( "- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $debug; 
    8181    return 1; 
    8282} 
     
    8686### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead 
    8787 
    88     TWiki::Func::writeDebug( "- ${pluginName}::beforeCommonTagsHandler( $_[2].$_[1] )" ) if $debug; 
     88    Foswiki::Func::writeDebug( "- ${pluginName}::beforeCommonTagsHandler( $_[2].$_[1] )" ) if $debug; 
    8989 
    9090    # handle all INCLUDETRANSLATION tags: 
     
    9696### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead 
    9797 
    98     TWiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $_[2].$_[1] )" ) if $debug; 
     98    Foswiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $_[2].$_[1] )" ) if $debug; 
    9999 
    100100    # handle our common tags: 
    101101    $_[0] =~ s/%TRANSLATIONS({(.*?)})?%/&handleTranslations($2)/ge; 
    102102    $_[0] =~ s/%CURRENTLANGUAGE%/&currentLanguage/ge; 
     103    $_[0] =~ s/%CURRENTLANGUAGESUFFIX%/&currentLanguageSuffix/ge; 
    103104    $_[0] =~ s/%DEFAULTLANGUAGE%/$defaultLanguage/ge; 
    104105    $_[0] =~ s/%BASETRANSLATION({(.*?)})?%/&handleBaseTranslation($2)/ge; 
     
    106107} 
    107108 
    108 # transform a language code into a suitable suffix for TWiki topics, 
    109 # by capitalizing the first letter and all the others lowercase. 
     109# transform a language code into a suitable suffix for topics, by capitalizing 
     110# the first letter and all the others lowercase. 
    110111# Examples: 
    111112#   pt-br -> Ptbr     EN -> En 
     
    146147} 
    147148 
     149# returns the current language suffix, or '' (empty string) if the current 
     150# language is the default one 
     151sub currentLanguageSuffix { 
     152  my $lang = currentLanguage(); 
     153  return ($lang eq $defaultLanguage) ? '' : normalizeLanguageName($lang); 
     154} 
     155 
    148156# list the translations of the current topic (or to that one passed as an 
    149157# argument). Depending on the arguments to the %TRANSLATIONS% tag, many options 
     
    157165 
    158166    # format for the items: 
    159     my $format = TWiki::Func::extractNameValuePair($params, "format") || "[[\$web.\$translation][\$language]]"; 
    160     my $missingFormat = TWiki::Func::extractNameValuePair($params, "missingformat") || $format; 
     167    my $format = Foswiki::Func::extractNameValuePair($params, "format") || "[[\$web.\$translation][\$language]]"; 
     168    my $missingFormat = Foswiki::Func::extractNameValuePair($params, "missingformat") || $format; 
    161169     
    162170    # other stuff: 
    163     my $userSeparator = TWiki::Func::extractNameValuePair($params, "separator") || " "; 
     171    my $userSeparator = Foswiki::Func::extractNameValuePair($params, "separator") || " "; 
    164172 
    165173    #  
    166     my $theTopic = TWiki::Func::extractNameValuePair($params) || TWiki::Func::extractNameValuePair($params,"topic") || $topic; 
     174    my $theTopic = Foswiki::Func::extractNameValuePair($params) || Foswiki::Func::extractNameValuePair($params,"topic") || $topic; 
    167175    my $theWeb; 
    168176    if ($theTopic =~ m/^([^.]+)\.([^.]+)/) { 
     
    176184 
    177185    # find out which translations we must list: 
    178     my $which = TWiki::Func::extractNameValuePair($params, "which") || "all"; 
     186    my $which = Foswiki::Func::extractNameValuePair($params, "which") || "all"; 
    179187    my @whichTranslations; 
    180188    if ($which eq "available") { 
     
    202210 
    203211    # wheter to use the format for available translations or for missing ones: 
    204     my $result = (TWiki::Func::topicExists($web, $translationTopic))?($format):($missingFormat); 
     212    my $result = (Foswiki::Func::topicExists($web, $translationTopic))?($format):($missingFormat); 
    205213 
    206214    # substitute the variables: 
     
    220228    my $theLang = currentLanguage(); 
    221229     
    222     my $theTopic = TWiki::Func::extractNameValuePair($params); 
     230    my $theTopic = Foswiki::Func::extractNameValuePair($params); 
    223231    my $theWeb; 
    224232    if ($theTopic =~ m/^([^.]+)\.([^.]+)/) { 
     
    236244 
    237245    # undef is ok, meaning current revision: 
    238     my $theRev = TWiki::Func::extractNameValuePair($params, "rev"); 
     246    my $theRev = Foswiki::Func::extractNameValuePair($params, "rev"); 
    239247 
    240248    my $args = "\"$theWeb.$theTopic\""; 
     
    268276    my $script = $ENV{SCRIPT_NAME} || '/view';  
    269277 
    270     if (($script =~ m#/view(auth)?$#) and (! $ENV{QUERY_STRING})) { 
    271         my $query = TWiki::Func::getCgiQuery(); 
     278    if (($script =~ m/\/view(auth)?$/) and (! $ENV{QUERY_STRING})) { 
     279        my $query = Foswiki::Func::getCgiQuery(); 
    272280     
    273281        # several checks 
    274282        my $baseTopicName = findBaseTopicName(); 
    275         my $baseUrl = TWiki::Func::getViewUrl($web, $baseTopicName); 
    276         my $editUrl = TWiki::Func::getScriptUrl($web, $baseTopicName, 'edit'); 
     283        my $baseUrl = Foswiki::Func::getViewUrl($web, $baseTopicName); 
     284        my $editUrl = Foswiki::Func::getScriptUrl($web, $baseTopicName, 'edit'); 
    277285        my $origin = $query->referer() || ''; 
    278286         
     
    289297              # actually do the redirect: 
    290298              my $bestTranslationTopic = findBaseTopicName() . (($best eq $defaultLanguage)?'':(normalizeLanguageName($best))); 
    291               my $url = TWiki::Func::getViewUrl($web,$bestTranslationTopic); 
    292               TWiki::Func::redirectCgiQuery($query, $url); 
     299              my $url = Foswiki::Func::getViewUrl($web,$bestTranslationTopic); 
     300              Foswiki::Func::redirectCgiQuery($query, $url); 
    293301            } 
    294302        } 
     
    326334         
    327335        # is that translation available? 
    328         $exists = TWiki::Func::topicExists($web, $theTopic . $norm); 
     336        $exists = Foswiki::Func::topicExists($web, $theTopic . $norm); 
    329337 
    330338        # what kind (available or not) are we looking for? 
     
    339347sub handleBaseTranslation { 
    340348    my $params = shift; 
    341     my $myTopic = TWiki::Func::extractNameValuePair($params, "topic") || $topic; 
     349    my $myTopic = Foswiki::Func::extractNameValuePair($params, "topic") || $topic; 
    342350    return findBaseTopicName($myTopic); 
    343351} 
     
    347355  my $params = shift; 
    348356  my $lang = currentLanguage(); 
    349   return (TWiki::Func::extractNameValuePair($params, $lang)); 
     357  return (Foswiki::Func::extractNameValuePair($params, $lang)); 
    350358} 
    351359 
Note: See TracChangeset for help on using the changeset viewer.