use "locale.str" to convert float to str

This commit is contained in:
Martin Wagner 2021-07-04 16:26:18 +02:00
parent 734836711f
commit 1e7a010bec

View File

@ -30,7 +30,10 @@ import os
import sys import sys
import re import re
import locale import locale
locale.setlocale(locale.LC_ALL, "") try:
locale.setlocale(locale.LC_ALL, "")
except locale.Error as e:
print(e)
from gettext import gettext as _, ngettext, textdomain, bindtextdomain from gettext import gettext as _, ngettext, textdomain, bindtextdomain
textdomain("mpdevil") textdomain("mpdevil")
if os.path.isfile("/.flatpak-info"): # test for flatpak environment if os.path.isfile("/.flatpak-info"): # test for flatpak environment
@ -458,11 +461,11 @@ class ClientHelper():
except: except:
int_chan=0 int_chan=0
try: try:
freq=str(int(samplerate)/1000) freq=locale.str(int(samplerate)/1000)
except: except:
freq=samplerate freq=samplerate
channels=ngettext("{channels} channel", "{channels} channels", int_chan).format(channels=channels) channels=ngettext("{channels} channel", "{channels} channels", int_chan).format(channels=channels)
return "{} kHz, {} bit, {}".format(freq, bits, channels) return "{} kHz • {} bit • {}".format(freq, bits, channels)
def song_to_str_dict(song): # converts tags with multiple values to comma separated strings def song_to_str_dict(song): # converts tags with multiple values to comma separated strings
return_song={} return_song={}
@ -1569,16 +1572,15 @@ class SongPopover(Gtk.Popover):
song=ClientHelper.song_to_str_dict(self._client.get_metadata(uri)) song=ClientHelper.song_to_str_dict(self._client.get_metadata(uri))
song.pop("time", None) song.pop("time", None)
for tag, value in song.items(): for tag, value in song.items():
tooltip=value.replace("&", "&")
if tag == "duration": if tag == "duration":
self._store.append([tag+":", ClientHelper.seconds_to_display_time(int(float(value))), tooltip]) self._store.append([tag+":", ClientHelper.seconds_to_display_time(int(float(value))), locale.str(float(value))])
elif tag == "last-modified": elif tag == "last-modified":
time=datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ") time=datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
self._store.append([tag+":", time.strftime("%a %d %B %Y, %H%M UTC"), tooltip]) self._store.append([tag+":", time.strftime("%a %d %B %Y, %H%M UTC"), value])
elif tag == "format": elif tag == "format":
self._store.append([tag+":", ClientHelper.convert_audio_format(value), tooltip]) self._store.append([tag+":", ClientHelper.convert_audio_format(value), value])
else: else:
self._store.append([tag+":", value, tooltip]) self._store.append([tag+":", value, value.replace("&", "&")])
abs_path=self._client.get_absolute_path(uri) abs_path=self._client.get_absolute_path(uri)
if abs_path is None: # show open with button when song is on the same computer if abs_path is None: # show open with button when song is on the same computer
self._open_button_revealer.set_reveal_child(False) self._open_button_revealer.set_reveal_child(False)
@ -2747,7 +2749,7 @@ class AudioType(Gtk.Label):
self._format, self._brate, self._file_type=("::", 0.0, "") self._format, self._brate, self._file_type=("::", 0.0, "")
def _refresh(self, *args): def _refresh(self, *args):
string="{} kb/s, {}, {}".format(self._brate, ClientHelper.convert_audio_format(self._format), self._file_type) string="{} kb/s • {} • {}".format(locale.str(self._brate), ClientHelper.convert_audio_format(self._format), self._file_type)
self.set_text(string) self.set_text(string)
def _on_audio(self, emitter, audio_format): def _on_audio(self, emitter, audio_format):
@ -2760,7 +2762,8 @@ class AudioType(Gtk.Label):
def _on_song_changed(self, *args): def _on_song_changed(self, *args):
try: try:
self._file_type=self._client.currentsong()["file"].split(".")[-1].split("/")[0] mime=Gio.content_type_guess(self._client.currentsong()["file"])[0]
self._file_type=Gio.content_type_get_description(mime)
self._refresh() self._refresh()
except: except:
pass pass