lib/TWiki/Plurals.pm
changeset 0 414e01d06fd5
child 1 e2915a7cbdfa
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/TWiki/Plurals.pm	Sat Jan 26 15:50:53 2008 +0100
     1.3 @@ -0,0 +1,79 @@
     1.4 +# TWiki Enterprise Collaboration Platform, http://TWiki.org/
     1.5 +#
     1.6 +# Copyright (C) 1999-2007 TWiki Contributors. All Rights Reserved.
     1.7 +# TWiki Contributors are listed in the AUTHORS file in the root of
     1.8 +# this distribution.
     1.9 +# NOTE: Please extend that file, not this notice.
    1.10 +#
    1.11 +# Additional copyrights apply to some or all of the code in this
    1.12 +# file as follows:
    1.13 +# Copyright (C) 2005 Martin Cleaver.
    1.14 +#
    1.15 +# This program is free software; you can redistribute it and/or
    1.16 +# modify it under the terms of the GNU General Public License
    1.17 +# as published by the Free Software Foundation; either version 2
    1.18 +# of the License, or (at your option) any later version. For
    1.19 +# more details read LICENSE in the root of this distribution.
    1.20 +#
    1.21 +# This program is distributed in the hope that it will be useful,
    1.22 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.23 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.24 +#
    1.25 +# As per the GPL, removal of this notice is prohibited.
    1.26 +
    1.27 +=pod
    1.28 +
    1.29 +---+ package TWiki::Plurals
    1.30 +
    1.31 +Handle conversion of plural topic names to singular form.
    1.32 +
    1.33 +=cut
    1.34 +
    1.35 +package TWiki::Plurals;
    1.36 +
    1.37 +use strict;
    1.38 +
    1.39 +=pod
    1.40 +
    1.41 +---++ StaticMethod singularForm($web, $pluralForm) -> $singularForm
    1.42 +
    1.43 +Try to singularise plural topic name.
    1.44 +   * =$web= - the web the topic must be in
    1.45 +   * =$pluralForm= - topic name
    1.46 +Returns undef if no singular form exists, otherwise returns the
    1.47 +singular form of the topic
    1.48 +
    1.49 +I18N - Only apply plural processing if site language is English, or
    1.50 +if a built-in English-language web (Main, TWiki or Plugins).  Plurals
    1.51 +apply to names ending in 's', where topic doesn't exist with plural
    1.52 +name.
    1.53 +
    1.54 +SMELL: this is highly langauge specific, and shoud be overridable
    1.55 +on a per-installation basis.
    1.56 +
    1.57 +=cut
    1.58 +
    1.59 +sub singularForm {
    1.60 +    my( $web, $pluralForm ) = @_;
    1.61 +    $web =~ s#\.#/#go;
    1.62 +
    1.63 +    # SMELL Plural processing should be set per web
    1.64 +    # SMELL Lang settings should be set per web
    1.65 +    return undef unless( $TWiki::cfg{PluralToSingular} );
    1.66 +    return undef unless( $pluralForm =~ /s$/ );
    1.67 +    return undef unless( !defined($TWiki::cfg{Site}{Lang}) or
    1.68 +                           $TWiki::cfg{Site}{Lang} eq 'en'
    1.69 +                             or $web eq $TWiki::cfg{UsersWebName}
    1.70 +                               or $web eq $TWiki::cfg{SystemWebName} );
    1.71 +    # Topic name is plural in form
    1.72 +    my $singularForm = $pluralForm;
    1.73 +    $singularForm =~ s/ies$/y/;      # plurals like policy / policies
    1.74 +    $singularForm =~ s/sses$/ss/;    # plurals like address / addresses
    1.75 +    $singularForm =~ s/ches$/ch/;    # plurals like search / searches
    1.76 +    $singularForm =~ s/(oes|os)$/o/; # plurals like veto / vetoes
    1.77 +    $singularForm =~ s/([Xx])es$/$1/;# plurals like box / boxes
    1.78 +    $singularForm =~ s/([^s])s$/$1/; # others, excluding ss like address(es)
    1.79 +    return $singularForm
    1.80 +}
    1.81 +
    1.82 +1;