2018-08-16 02:01:49 +00:00
|
|
|
from get_argv import config_dir
|
|
|
|
|
2017-09-16 00:49:46 +00:00
|
|
|
import sqlite3
|
|
|
|
import pycountry
|
2017-10-20 12:59:21 +00:00
|
|
|
import os
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2018-07-30 00:42:24 +00:00
|
|
|
def load_language_in_db():
|
|
|
|
# Get languages list in langs tuple
|
|
|
|
langs = [[lang.alpha_3,lang.alpha_2,lang.name]
|
|
|
|
for lang in pycountry.languages
|
|
|
|
if hasattr(lang, 'alpha_2')]
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2018-07-30 00:42:24 +00:00
|
|
|
# Open database connection
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2018-07-30 00:42:24 +00:00
|
|
|
# Insert languages in database table
|
|
|
|
c.executemany('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', langs)
|
|
|
|
c.execute('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', ('pob','pb','Brazilian Portuguese'))
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2018-07-30 00:42:24 +00:00
|
|
|
# Commit changes to database table
|
|
|
|
db.commit()
|
2017-09-16 00:49:46 +00:00
|
|
|
|
2018-07-30 00:42:24 +00:00
|
|
|
# Close database connection
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
def language_from_alpha2(lang):
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
|
|
|
try:
|
|
|
|
result = c.execute('''SELECT name FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0]
|
|
|
|
except:
|
|
|
|
result = None
|
|
|
|
db.close
|
|
|
|
return result
|
|
|
|
|
|
|
|
def language_from_alpha3(lang):
|
2018-07-30 02:11:34 +00:00
|
|
|
if lang == 'fre':
|
|
|
|
lang = 'fra'
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
|
|
|
try:
|
|
|
|
result = c.execute('''SELECT name FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0]
|
|
|
|
except:
|
|
|
|
result = None
|
|
|
|
db.close
|
|
|
|
return result
|
|
|
|
|
|
|
|
def alpha2_from_alpha3(lang):
|
2018-07-30 02:11:34 +00:00
|
|
|
if lang == 'fre':
|
|
|
|
lang = 'fra'
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
|
|
|
try:
|
|
|
|
result = c.execute('''SELECT code2 FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0]
|
|
|
|
except:
|
|
|
|
result = None
|
|
|
|
db.close
|
|
|
|
return result
|
|
|
|
|
|
|
|
def alpha2_from_language(lang):
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
|
|
|
try:
|
|
|
|
result = c.execute('''SELECT code2 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0]
|
|
|
|
except:
|
|
|
|
result = None
|
|
|
|
db.close
|
|
|
|
return result
|
|
|
|
|
|
|
|
def alpha3_from_alpha2(lang):
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
|
|
|
try:
|
|
|
|
result = c.execute('''SELECT code3 FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0]
|
|
|
|
except:
|
|
|
|
result = None
|
|
|
|
db.close
|
|
|
|
return result
|
|
|
|
|
|
|
|
def alpha3_from_language(lang):
|
2018-08-16 02:01:49 +00:00
|
|
|
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
|
2018-07-30 00:42:24 +00:00
|
|
|
c = db.cursor()
|
|
|
|
try:
|
|
|
|
result = c.execute('''SELECT code3 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0]
|
|
|
|
except:
|
|
|
|
result = None
|
|
|
|
db.close
|
|
|
|
return result
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
load_language_in_db()
|