Skip to content

Commit

Permalink
Use Qt's locale instead of Python's
Browse files Browse the repository at this point in the history
Python locale (on Windows) is a mess, and doesn't return a proper RFC
1766 language code.
  • Loading branch information
jim-easterbrook committed Aug 26, 2024
1 parent 2bbd313 commit 2dd8d27
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 51 deletions.
6 changes: 2 additions & 4 deletions src/photini/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
## <http://www.gnu.org/licenses/>.

from collections import defaultdict
import locale
import logging
import os

Expand Down Expand Up @@ -97,9 +96,8 @@ def query(self, params):

def get_address(self, coords):
params = {'q': '{:.5f},{:.5f}'.format(*coords)}
lang, encoding = locale.getlocale()
if lang:
params['language'] = lang
lang = self.app.locale.bcp47Name()
params['language'] = lang
results = self.cached_query(params)
if not results:
return None
Expand Down
21 changes: 9 additions & 12 deletions src/photini/azuremap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# along with Photini. If not, see <http://www.gnu.org/licenses/>.

import base64
import locale
import logging
import os

Expand Down Expand Up @@ -67,11 +66,10 @@ def search(self, search_string, bounds=None):
north, east, south, west = bounds
params['bbox'] = '{:.4f},{:.4f},{:.4f},{:.4f}'.format(
west, south, east, north)
lang, encoding = locale.getlocale()
if lang:
lang, sep, country = lang.partition('_')
if country:
params['view'] = country
lang = self.app.locale.bcp47Name()
lang, sep, country = lang.partition('-')
if country:
params['view'] = country
for feature in self.cached_query(
params, 'https://atlas.microsoft.com/geocode'):
properties = feature['properties']
Expand Down Expand Up @@ -150,10 +148,9 @@ def get_options(self):
'subscriptionKey': self.api_key,
},
}
language, encoding = locale.getlocale()
if language:
options['language'] = language.replace('_', '-')
language, sep, country = language.partition('_')
if country:
options['View'] = country
language = self.locale().bcp47Name()
options['language'] = language
language, sep, country = language.partition('_')
if country:
options['View'] = country
return options
16 changes: 6 additions & 10 deletions src/photini/bingmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
## along with this program. If not, see
## <http://www.gnu.org/licenses/>.

import locale
import logging

import requests
Expand Down Expand Up @@ -76,9 +75,8 @@ def search(self, search_string, bounds=None):
'query' : search_string,
'maxRes': '20',
}
lang, encoding = locale.getlocale()
if lang:
params['culture'] = lang.replace('_', '-')
lang = self.app.locale.bcp47Name()
params['culture'] = lang
if bounds:
north, east, south, west = bounds
params['userMapView'] = '{:.4f},{:.4f},{:.4f},{:.4f}'.format(
Expand Down Expand Up @@ -113,12 +111,10 @@ def get_geocoder(self):
def get_head(self):
url = 'http://www.bing.com/api/maps/mapcontrol?callback=initialize'
url += '&key=' + self.api_key
lang, encoding = locale.getlocale()
if lang:
culture = lang.replace('_', '-')
url += '&setMkt=' + culture
language, sep, region = culture.partition('-')
url += '&setLang=' + language
lang = self.locale().bcp47Name()
url += '&setMkt=' + lang
lang, sep, region = lang.partition('-')
url += '&setLang=' + lang
if self.app.options.test:
url += '&branch=experimental'
return ''' <script type="text/javascript"
Expand Down
1 change: 1 addition & 0 deletions src/photini/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ def __init__(self, options, initial_files):
# create shared global objects
self.app = QtWidgets.QApplication.instance()
self.app.config_store = ConfigStore('editor', parent=self)
self.app.locale = QtCore.QLocale.system()
self.app.spell_check = SpellCheck(parent=self)
if GpxImporter:
self.app.gpx_importer = GpxImporter(parent=self)
Expand Down
17 changes: 7 additions & 10 deletions src/photini/googlemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
## along with this program. If not, see
## <http://www.gnu.org/licenses/>.

import locale
import logging
import re

Expand Down Expand Up @@ -68,9 +67,8 @@ def get_altitude(self, coords):

def search(self, search_string, bounds=None):
params = {'address': search_string}
lang, encoding = locale.getlocale()
if lang:
params['language'] = lang.replace('_', '-')
lang = self.app.locale.bcp47Name()
params['language'] = lang
if bounds:
north, east, south, west = bounds
params['bounds'] = '{:.4f},{:.4f}|{:.4f},{:.4f}'.format(
Expand Down Expand Up @@ -118,12 +116,11 @@ def get_head(self):
if self.app.options.test:
url += '&v=beta'
url += '&key=' + self.api_key
lang, encoding = locale.getlocale()
if lang:
language, sep, region = lang.replace('_', '-').partition('-')
url += '&language=' + language
if region:
url += '&region=' + region
lang = self.locale().bcp47Name()
lang, sep, region = lang.partition('-')
url += '&language=' + lang
if region:
url += '&region=' + region
return ''' <script type="text/javascript">
const use_old_markers = {use_old_markers};
</script>
Expand Down
18 changes: 7 additions & 11 deletions src/photini/mapboxmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
## along with this program. If not, see
## <http://www.gnu.org/licenses/>.

import locale
import logging

import requests
Expand All @@ -40,9 +39,8 @@ def query(self, params):
del params['query']
params['access_token'] = self.api_key
params['autocomplete '] = 'false'
lang, encoding = locale.getlocale()
if lang:
params['language'] = lang
lang = self.app.locale.bcp47Name()
params['language'] = lang
query += '.json'
url = 'https://api.mapbox.com/geocoding/v5/mapbox.places/' + query
with Busy():
Expand Down Expand Up @@ -159,11 +157,9 @@ def get_body(self, text_dir):

def get_options(self):
options = {'accessToken': self.api_key}
lang, encoding = locale.getlocale()
if lang:
lang = lang.replace('_', '-')
options['language'] = lang
language, sep, region = lang.partition('-')
if region:
options['worldview'] = region
lang = self.locale().bcp47Name()
options['language'] = lang
lang, sep, region = lang.partition('-')
if region:
options['worldview'] = region
return options
5 changes: 1 addition & 4 deletions src/photini/photinimap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
## <http://www.gnu.org/licenses/>.

from datetime import timezone
import locale
import logging
import os
import pickle
Expand Down Expand Up @@ -306,9 +305,7 @@ def get_options(self):
def initialise(self):
lat, lng = self.app.config_store.get('map', 'centre', (51.0, 0.0))
zoom = float(self.app.config_store.get('map', 'zoom', 11))
lang, encoding = locale.getlocale()
lang = lang or 'en-GB'
lang = lang.replace('_', '-')
lang = self.locale().bcp47Name()
text_dir = ('ltr', 'rtl')[
self.use_layout_direction and
self.layoutDirection() == Qt.LayoutDirection.RightToLeft]
Expand Down

0 comments on commit 2dd8d27

Please sign in to comment.