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