Created
January 8, 2013 14:58
-
-
Save tobynet/4484382 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# An `ad-hoc` script to change user interface language | |
# for Anki2 on Ubuntu 12.04 desktop. | |
# Usage: anki2-change-defaultLang-to-en.py [lang] | |
# | |
# Examples: | |
# # Change to english interface | |
# $ anki2-change-defaultLang-to-en.py en | |
# | |
# # Change to japanese interface | |
# $ anki2-change-defaultLang-to-en.py ja | |
# | |
# Available language codes are in following URL. | |
# https://github.com/dae/anki/blob/master/anki/lang.py | |
# | |
import sys | |
import os | |
import sqlite3 | |
import cPickle | |
# set lang | |
if 2 <= len(sys.argv): | |
YOUR_LANG = sys.argv[1] | |
else: | |
YOUR_LANG = "en" # english | |
def fetch_meta_info(db): | |
# Get default profile | |
data = db.execute("select data from profiles where name='_global'") | |
# encode('utf-8') is for fixing "TypeError: must be string, not unicode" | |
meta = cPickle.loads(data.fetchone()[0].encode('utf-8')) | |
return meta | |
# Open config DB | |
db = sqlite3.connect(os.path.expanduser("~/Anki/prefs.db")) | |
try: | |
meta = fetch_meta_info(db) | |
old_lang = meta['defaultLang'] | |
if old_lang == YOUR_LANG: | |
print "INFO: Already changed lang to `%s`" % YOUR_LANG | |
exit() | |
# Change lang to YOUR_LANG you want | |
meta['defaultLang'] = YOUR_LANG | |
# Update config DB!! | |
db.execute("update profiles set data = ? where name = ?", | |
(cPickle.dumps(meta), "_global")) | |
db.commit() | |
# Check and notify | |
new_lang = fetch_meta_info(db)['defaultLang'] | |
if new_lang != YOUR_LANG: | |
raise Exception("ERROR: Could not change defaultLang") | |
print "INFO: Updated interface lang `%s` to `%s`" % (old_lang, new_lang) | |
finally: | |
db.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, this really helped!
Make sure to close Anki before running the script.