mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 08:43:25 +08:00
UX: Use user locale for locations. (#6527)
* UX: Use user locale for locations. * DEV: Added MaxMindDB test data and fixed test.
This commit is contained in:
parent
8e274f7296
commit
effbef7d0b
|
@ -20,7 +20,7 @@ module UserAuthTokensMixin
|
|||
end
|
||||
|
||||
def location
|
||||
ipinfo = DiscourseIpInfo.get(client_ip)
|
||||
ipinfo = DiscourseIpInfo.get(client_ip, I18n.locale)
|
||||
|
||||
location = [ipinfo[:city], ipinfo[:region], ipinfo[:country]].reject { |x| x.blank? }.join(", ")
|
||||
return I18n.t('staff_action_logs.unknown') if location.blank?
|
||||
|
|
|
@ -4,8 +4,12 @@ class DiscourseIpInfo
|
|||
include Singleton
|
||||
|
||||
def initialize
|
||||
open_db(File.join(Rails.root, 'vendor', 'data'))
|
||||
end
|
||||
|
||||
def open_db(path)
|
||||
begin
|
||||
@mmdb_filename = File.join(Rails.root, 'vendor', 'data', 'GeoLite2-City.mmdb')
|
||||
@mmdb_filename = File.join(path, 'GeoLite2-City.mmdb')
|
||||
@mmdb = MaxMindDB.new(@mmdb_filename, MaxMindDB::LOW_MEMORY_FILE_READER)
|
||||
@cache = LruRedux::ThreadSafeCache.new(1000)
|
||||
rescue Errno::ENOENT => e
|
||||
|
@ -15,7 +19,7 @@ class DiscourseIpInfo
|
|||
end
|
||||
end
|
||||
|
||||
def lookup(ip)
|
||||
def lookup(ip, locale = :en)
|
||||
return {} unless @mmdb
|
||||
|
||||
begin
|
||||
|
@ -26,22 +30,28 @@ class DiscourseIpInfo
|
|||
|
||||
return {} if !result || !result.found?
|
||||
|
||||
locale = locale.to_s.sub('_', '-')
|
||||
|
||||
{
|
||||
country: result.country.name,
|
||||
country: result.country.name(locale) || result.country.name,
|
||||
country_code: result.country.iso_code,
|
||||
region: result.subdivisions.most_specific.name,
|
||||
city: result.city.name,
|
||||
region: result.subdivisions.most_specific.name(locale) || result.subdivisions.most_specific.name,
|
||||
city: result.city.name(locale) || result.city.name,
|
||||
}
|
||||
end
|
||||
|
||||
def get(ip)
|
||||
def get(ip, locale = :en)
|
||||
return {} unless @mmdb
|
||||
|
||||
ip = ip.to_s
|
||||
@cache[ip] ||= lookup(ip)
|
||||
@cache["#{ip}-#{locale}"] ||= lookup(ip, locale)
|
||||
end
|
||||
|
||||
def self.get(ip)
|
||||
instance.get(ip)
|
||||
def self.open_db(path)
|
||||
instance.open_db(path)
|
||||
end
|
||||
|
||||
def self.get(ip, locale = :en)
|
||||
instance.get(ip, locale)
|
||||
end
|
||||
end
|
||||
|
|
BIN
spec/fixtures/mmdb/GeoLite2-City.mmdb
vendored
Normal file
BIN
spec/fixtures/mmdb/GeoLite2-City.mmdb
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
23
spec/serializers/user_auth_token_serializer_spec.rb
Normal file
23
spec/serializers/user_auth_token_serializer_spec.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe UserAuthTokenSerializer do
|
||||
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:token) { UserAuthToken.generate!(user_id: user.id, client_ip: '2a02:ea00::') }
|
||||
|
||||
before(:each) do
|
||||
DiscourseIpInfo.open_db(File.join(Rails.root, 'spec', 'fixtures', 'mmdb'))
|
||||
end
|
||||
|
||||
it 'serializes user auth tokens with respect to user locale' do
|
||||
I18n.locale = 'de'
|
||||
json = UserAuthTokenSerializer.new(token, scope: Guardian.new(user), root: false).as_json
|
||||
expect(json[:location]).to include('Schweiz')
|
||||
end
|
||||
|
||||
it 'correctly translates Discourse locale to MaxMindDb locale' do
|
||||
I18n.locale = 'zh_CN'
|
||||
json = UserAuthTokenSerializer.new(token, scope: Guardian.new(user), root: false).as_json
|
||||
expect(json[:location]).to include('瑞士')
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user