lib/TWiki/Plurals.pm
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
equal deleted inserted replaced
-1:000000000000 0:414e01d06fd5
       
     1 # TWiki Enterprise Collaboration Platform, http://TWiki.org/
       
     2 #
       
     3 # Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.
       
     4 # TWiki Contributors are listed in the AUTHORS file in the root of
       
     5 # this distribution.
       
     6 # NOTE: Please extend that file, not this notice.
       
     7 #
       
     8 # Additional copyrights apply to some or all of the code in this
       
     9 # file as follows:
       
    10 # Copyright (C) 2005 Martin Cleaver.
       
    11 #
       
    12 # This program is free software; you can redistribute it and/or
       
    13 # modify it under the terms of the GNU General Public License
       
    14 # as published by the Free Software Foundation; either version 2
       
    15 # of the License, or (at your option) any later version. For
       
    16 # more details read LICENSE in the root of this distribution.
       
    17 #
       
    18 # This program is distributed in the hope that it will be useful,
       
    19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
    21 #
       
    22 # As per the GPL, removal of this notice is prohibited.
       
    23 
       
    24 =pod
       
    25 
       
    26 ---+ package TWiki::Plurals
       
    27 
       
    28 Handle conversion of plural topic names to singular form.
       
    29 
       
    30 =cut
       
    31 
       
    32 package TWiki::Plurals;
       
    33 
       
    34 use strict;
       
    35 
       
    36 =pod
       
    37 
       
    38 ---++ StaticMethod singularForm($web, $pluralForm) -> $singularForm
       
    39 
       
    40 Try to singularise plural topic name.
       
    41    * =$web= - the web the topic must be in
       
    42    * =$pluralForm= - topic name
       
    43 Returns undef if no singular form exists, otherwise returns the
       
    44 singular form of the topic
       
    45 
       
    46 I18N - Only apply plural processing if site language is English, or
       
    47 if a built-in English-language web (Main, TWiki or Plugins).  Plurals
       
    48 apply to names ending in 's', where topic doesn't exist with plural
       
    49 name.
       
    50 
       
    51 SMELL: this is highly langauge specific, and shoud be overridable
       
    52 on a per-installation basis.
       
    53 
       
    54 =cut
       
    55 
       
    56 sub singularForm {
       
    57     my( $web, $pluralForm ) = @_;
       
    58     $web =~ s#\.#/#go;
       
    59 
       
    60     # SMELL Plural processing should be set per web
       
    61     # SMELL Lang settings should be set per web
       
    62     return undef unless( $TWiki::cfg{PluralToSingular} );
       
    63     return undef unless( $pluralForm =~ /s$/ );
       
    64     return undef unless( !defined($TWiki::cfg{Site}{Lang}) or
       
    65                            $TWiki::cfg{Site}{Lang} eq 'en'
       
    66                              or $web eq $TWiki::cfg{UsersWebName}
       
    67                                or $web eq $TWiki::cfg{SystemWebName} );
       
    68     # Topic name is plural in form
       
    69     my $singularForm = $pluralForm;
       
    70     $singularForm =~ s/ies$/y/;      # plurals like policy / policies
       
    71     $singularForm =~ s/sses$/ss/;    # plurals like address / addresses
       
    72     $singularForm =~ s/ches$/ch/;    # plurals like search / searches
       
    73     $singularForm =~ s/(oes|os)$/o/; # plurals like veto / vetoes
       
    74     $singularForm =~ s/([Xx])es$/$1/;# plurals like box / boxes
       
    75     $singularForm =~ s/([^s])s$/$1/; # others, excluding ss like address(es)
       
    76     return $singularForm
       
    77 }
       
    78 
       
    79 1;