~ubuntu-branches/ubuntu/vivid/zonecheck/vivid

« back to all changes in this revision

Viewing changes to .pc/20locale-rb.dpatch/zc/locale.rb

  • Committer: Package Import Robot
  • Author(s): Sebastien Delafond
  • Date: 2012-06-18 15:44:33 UTC
  • Revision ID: package-import@ubuntu.com-20120618154433-hxy9iuf4fa9k2jvv
Tags: 3.0.3-2
* Fix problems related to ruby 1.9, thanks to patches from Antonio
  Terceiro <terceiro@debian.org> (Closes: #676109, #676173)
* Bump-up Standards revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: locale.rb,v 1.5 2010/06/29 12:08:35 chabannf Exp $
2
 
 
3
 
4
 
# CONTACT     : zonecheck@nic.fr
5
 
# AUTHOR      : Stephane D'Alu <sdalu@nic.fr>
6
 
#
7
 
# CREATED     : 2003/08/29 14:10:22
8
 
# REVISION    : $Revision: 1.5 $ 
9
 
# DATE        : $Date: 2010/06/29 12:08:35 $
10
 
#
11
 
# CONTRIBUTORS: (see also CREDITS file)
12
 
#
13
 
#
14
 
# LICENSE     : GPL v3
15
 
# COPYRIGHT   : AFNIC (c) 2003
16
 
#
17
 
# This file is part of ZoneCheck.
18
 
#
19
 
# ZoneCheck is free software; you can redistribute it and/or modify it
20
 
# under the terms of the GNU General Public License as published by
21
 
# the Free Software Foundation; either version 3 of the License, or
22
 
# (at your option) any later version.
23
 
24
 
# ZoneCheck is distributed in the hope that it will be useful, but
25
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
26
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
 
# General Public License for more details.
28
 
#
29
 
# You should have received a copy of the GNU General Public License
30
 
# along with ZoneCheck; if not, write to the Free Software Foundation,
31
 
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
 
#
33
 
module ZoneCheck
34
 
class Locale
35
 
    LANGRegex   = /^(\w+?)(?:_(\w+))?(?:\.([\w\-]+))?$/
36
 
 
37
 
    #
38
 
    # Normalize lang
39
 
    #  (and raise exception is the parameter is suspicious)
40
 
    #  The settings are based on: LanguageCode_CountryCode.Encoding
41
 
    #
42
 
    def self.normlang(lng)
43
 
        unless md = LANGRegex.match(lng)
44
 
            raise ArgumentError, "Suspicious language selection: #{lng}"
45
 
        end
46
 
        lang  =       md[1].downcase
47
 
        lang += '_' + md[2].upcase   if md[2]
48
 
        lang += '.' + md[3].downcase if md[3]
49
 
        lang.untaint
50
 
    end
51
 
 
52
 
    #
53
 
    # Split lang between Language, Country, Encoding
54
 
    #
55
 
    def self.splitlang(lng)
56
 
        unless md = LANGRegex.match(lng)
57
 
            raise ArgumentError, "Suspicious language selection: #{lng}"
58
 
        end
59
 
        [ md[1], md[2], md[3] ]
60
 
    end
61
 
 
62
 
 
63
 
    #
64
 
    # Initializer
65
 
    #
66
 
    def initialize
67
 
        @actions        = {}
68
 
 
69
 
        @lang           = nil
70
 
        @language       = nil
71
 
        @country        = nil
72
 
        @encoding       = nil
73
 
 
74
 
      if ENV['LANG']
75
 
        lng = ENV['LANG'] 
76
 
        ln, ct, en = ZoneCheck::Locale::splitlang(ZoneCheck::Locale::normlang(lng))
77
 
        evlist = []
78
 
        evlist << 'lang'  if (@language != ln) || (@country != ct)
79
 
        evlist << 'encoding'  if (@encoding != en)
80
 
        @lang, @language, @country, @encoding = lng, ln, ct, en
81
 
        $dbg.msg(DBG::LOCALE) { "locale set to #{lng}" }
82
 
        notify(*evlist)
83
 
      end
84
 
    end
85
 
 
86
 
    attr_reader :lang, :language, :country, :encoding
87
 
 
88
 
    def lang=(lng)
89
 
      ln, ct, en = ZoneCheck::Locale::splitlang(ZoneCheck::Locale::normlang(lng))
90
 
      if($supported_languages.include?(ln.downcase))
91
 
        evlist = []
92
 
        evlist << 'lang'        if (@language != ln) || (@country != ct)
93
 
        evlist << 'encoding'    if (@encoding != en)
94
 
        @lang, @language, @country, @encoding = lng, ln, ct, en
95
 
        $dbg.msg(DBG::LOCALE) { "locale set to #{lng}" }
96
 
        notify(*evlist)
97
 
      else
98
 
        $dbg.msg(DBG::LOCALE) { "The given language (#{lng}) is not supported by ZoneCheck. " +
99
 
        "Here is a list of supported languages: " +
100
 
        $supported_languages.join(", ")   }
101
 
      end
102
 
    end
103
 
 
104
 
    def watch(event, action)
105
 
        (@actions[event] ||= []) << action
106
 
    end
107
 
 
108
 
    def notify(*event)
109
 
        event.each { |ev|
110
 
            @actions[ev].each { |a| a.call } if @actions.has_key?(ev) }
111
 
    end
112
 
end
113
 
end
 
 
b'\\ No newline at end of file'