Skip to content

Instantly share code, notes, and snippets.

@tobynet
Created January 8, 2013 14:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobynet/4484382 to your computer and use it in GitHub Desktop.
Save tobynet/4484382 to your computer and use it in GitHub Desktop.
#!/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()
@s3ththompson
Copy link

Thank you, this really helped!

Make sure to close Anki before running the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment