]> git.openstreetmap.org Git - rails.git/blob - app/helpers/browse_helper.rb
Try harder to find a name in the right language
[rails.git] / app / helpers / browse_helper.rb
1 module BrowseHelper
2   def printable_name(object, version=false)
3     if object.id.is_a?(Array)
4       id = object.id[0]
5     else
6       id = object.id
7     end
8     name = t 'printable_name.with_id', :id => id.to_s
9     if version
10       name = t 'printable_name.with_version', :id => name, :version => object.version.to_s
11     end
12
13     # don't look at object tags if redacted, so as to avoid giving
14     # away redacted version tag information.
15     unless object.redacted?
16       locale = I18n.locale.to_s
17
18       while locale =~ /-[^-]+/ and not object.tags.include? "name:#{I18n.locale}"
19         locale = locale.sub(/-[^-]+/, "")
20       end
21
22       if object.tags.include? "name:#{locale}"
23         name = t 'printable_name.with_name_html', :name => content_tag(:bdi, object.tags["name:#{locale}"].to_s ), :id => content_tag(:bdi, name)
24       elsif object.tags.include? 'name'
25         name = t 'printable_name.with_name_html', :name => content_tag(:bdi, object.tags['name'].to_s ), :id => content_tag(:bdi, name)
26       end
27     end
28
29     name
30   end
31
32   def link_class(type, object)
33     classes = [ type ]
34
35     if object.redacted?
36       classes << "deleted"
37     else
38       classes += icon_tags(object).flatten.map { |t| h(t) }
39       classes << "deleted" unless object.visible?
40     end
41
42     classes.join(" ")
43   end
44
45   def link_title(object)
46     if object.redacted?
47       ""
48     else
49       h(icon_tags(object).map { |k,v| k + '=' + v }.to_sentence)
50     end
51   end
52
53   def format_key(key)
54     if url = wiki_link("key", key)
55       link_to h(key), url, :title => t('browse.tag_details.wiki_link.key', :key => key)
56     else
57       h(key)
58     end
59   end
60
61   def format_value(key, value)
62     if wp = wikipedia_link(key, value)
63       link_to h(wp[:title]), wp[:url], :title => t('browse.tag_details.wikipedia_link', :page => wp[:title])
64     elsif url = wiki_link("tag", "#{key}=#{value}")
65       link_to h(value), url, :title => t('browse.tag_details.wiki_link.tag', :key => key, :value => value)
66     else
67       linkify h(value)
68     end
69   end
70
71   def type_and_paginated_count(type, pages)
72     if pages.page_count == 1
73       t "browse.changeset.#{type}",
74         :count => pages.item_count
75     else
76       t "browse.changeset.#{type}_paginated",
77         :x => pages.current_page.first_item,
78         :y => pages.current_page.last_item,
79         :count => pages.item_count
80     end
81   end
82
83 private
84
85   ICON_TAGS = [
86     "aeroway", "amenity", "barrier", "building", "highway", "historic", "landuse",
87     "leisure", "man_made", "natural", "railway", "shop", "tourism", "waterway"
88   ]
89
90   def icon_tags(object)
91     object.tags.find_all { |k,v| ICON_TAGS.include? k }
92   end
93
94   def wiki_link(type, lookup)
95     locale = I18n.locale.to_s
96
97     # update-wiki-pages does s/ /_/g on keys before saving them, we
98     # have to replace spaces with underscore so we'll link
99     # e.g. `source=Isle of Man Government aerial imagery (2001)' to
100     # the correct page.
101     lookup_us = lookup.tr(" ", "_")
102
103     if page = WIKI_PAGES[locale][type][lookup_us] rescue nil
104       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
105     elsif page = WIKI_PAGES["en"][type][lookup_us] rescue nil
106       url = "http://wiki.openstreetmap.org/wiki/#{page}?uselang=#{locale}"
107     end
108
109     return url
110   end
111
112   def wikipedia_link(key, value)
113     # Some k/v's are wikipedia=http://en.wikipedia.org/wiki/Full%20URL
114     return nil if value =~ /^https?:\/\//
115
116     if key == "wikipedia"
117       # This regex should match Wikipedia language codes, everything
118       # from de to zh-classical
119       if value =~ /^([a-z-]{2,12}):(.+)$/i
120         # Value is <lang>:<title> so split it up
121         # Note that value is always left as-is, see: https://trac.openstreetmap.org/ticket/4315
122         lang  = $1
123       else
124         # Value is <title> so default to English Wikipedia
125         lang = 'en'
126       end
127     elsif key =~ /^wikipedia:(\S+)$/
128       # Language is in the key, so assume value is the title
129       lang = $1
130     else
131       # Not a wikipedia key!
132       return nil
133     end
134
135     if value =~ /^([^#]*)(#.*)/ then
136       # Contains a reference to a section of the wikipedia article
137       # Must break it up to correctly build the url
138       value = $1
139       section = $2
140     else
141       section = ""
142     end
143
144     return {
145       :url => "http://#{lang}.wikipedia.org/wiki/#{value}?uselang=#{I18n.locale}#{section}",
146       :title => value + section
147     }
148   end
149 end