Change locale for Google Colab

Viewed 952

I want to change the local setting (to change the date format) in GoogleCollab

The following works for me in JupyterNotebook but not in GoogleColab:

locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')

It always returns the error: unsupported locale setting

I have already looked at many other solutions and tried everything. One solution to change only the time zone I have seen is this one:

'!rm /etc/localtime
!ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime
!date
3 Answers

I figured this one out after a long time:

  1. In Colab, you will have to install the desired locales. You do this with:
    !sudo dpkg-reconfigure locales
    • This will prompt for a numeric input, e.g. 268 and 269 for Hungarian.
      So you enter 268 269.
    • It will also prompt for the default locale, after installation. Here you will need to select your desired custom locale. This time, it is a numeric selection out of 3-5 options, depending, on how many have you selected at the previous step. In my case, I have selected 3, and the default locale became hu_HU.
  2. You need to restart the Colab runtime: Ctrl + M then .
  3. You need to activate the locale:
    import locale
    locale.setlocale(locale.LC_ALL, 'hu_HU') <- make sure you do it for the LC_ALL context.
  4. The custom locale is now ready to use with pandas:
    pd.to_datetime('2021-01-01').day_name() returns Friday, but
    pd.to_datetime('2021-01-01').day_name('hu_HU') returns Péntek

I wasn't successful using German locale on Google Colab, but desired formatting could be obtained as a combination of overriding locale for decimal separator and date formatting.

German formatting rules can be found here.

For custom string formatting nice cheatsheet is here.

from datetime import datetime, timedelta
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import locale

german_format_str_full = '%Y-%m-%d, %H.%M Uhr'
german_format_str_date = '%Y-%m-%d'

# genereting plot data, xs are dates with not obvious step
xs = np.arange(datetime(year=2021, month=11, day=28, hour=23, minute=59, second=59),
               datetime(year=2021, month=12, day=6, hour=23, minute=59, second=59),
               timedelta(hours=5,minutes=47,seconds=27))
ys = np.sin(np.arange(0,len(xs),1)) # whatever

# use overwritten locale for comma as decimal point -- German formatting
plt.rcParams['axes.formatter.use_locale'] = True
locale._override_localeconv["decimal_point"]= ','

# plot
fig, ax = plt.subplots(figsize=(9,4))
ax.plot(xs,ys, 'o-')

# set formatting string using mdates from matplotlib
ax.xaxis.set_major_formatter(mdates.DateFormatter(german_format_str_date))

# rotate formatted ticks or use autoformat 'fig.autofmt_xdate()'
plt.xticks(rotation=70)
plt.title('Google Colab plot with German locale style')
plt.show()

It gives me this plot:

Google Colab matplotlib plot with German locale style

If you need to check how formatting settings look like on your machine you can use locale.nl_langinfo(locale.D_T_FMT). For example:

import locale
from datetime import datetime

now = datetime.now()

#  find local date time formatting on Google Colab 
local_format_str = locale.nl_langinfo(locale.D_T_FMT)
print('local_format_str on Google Colab: ', local_format_str)
print('now in Google Colab default format:', now.strftime(local_format_str))

german_format_str_full = '%Y-%m-%d, %H.%M Uhr'
german_format_str_date = '%Y-%m-%d'
print('now in German format, full:',now.strftime(german_format_str_full))
print('now in German format, only date:',now.strftime(german_format_str_date))

ridiculous_format = '%Y->%m-->%d'
print('now ridiculous_format:',now.strftime(ridiculous_format))

Based on this answer I was able to load german locales. However it needs to be done in two steps: Installing new, german locale. Restarting kernel and loading german locale.

In short:

import os

# Install de_DE
!/usr/share/locales/install-language-pack de_DE
!dpkg-reconfigure locales

# Restart Python process to pick up the new locales
os.kill(os.getpid(), 9)

More detailed version: It turned out that the list of available locales is pretty short which can be checked like this:

import locale
from datetime import datetime

now = datetime.now()

#  find local date time formatting on Google Colab 
local_format_str = locale.nl_langinfo(locale.D_T_FMT)
print('local_format_str on Google Colab: ', local_format_str)
print('now in Google Colab default format:', now.strftime(local_format_str))

print('Loading avaliable locales via real names...')
for real_name in set(locale.locale_alias.values()):
  try:
    locale.setlocale(locale.LC_ALL, real_name)
    print('success: real_name = ', real_name)
  except:
    pass

print('Loading avaliable locales via aliases...')
for alias , real_name in locale.locale_alias.items():
  try:
    locale.setlocale(locale.LC_ALL, alias)
    print('success: alias = ' , alias, ' , real_name = ', real_name)
  except:
    pass

With output:

local_format_str on Google Colab:  %a %b %e %H:%M:%S %Y
now in Google Colab default format: Wed Dec  1 12:10:52 2021
Loading avaliable locales via real names...
success: real_name =  en_US.UTF-8
success: real_name =  C
Loading avaliable locales via aliases...

As we can see there is no german locale, so it needs to be installed with code:

import os

# Install de_DE
!/usr/share/locales/install-language-pack de_DE
!dpkg-reconfigure locales

# Restart Python process to pick up the new locales
os.kill(os.getpid(), 9)

giving an output:

Generating locales (this might take a while)...
  de_DE.ISO-8859-1... done
Generation complete.
dpkg-trigger: error: must be called from a maintainer script (or with a --by-package option)

Type dpkg-trigger --help for help about this utility.
Generating locales (this might take a while)...
  de_DE.ISO-8859-1... done
  en_US.UTF-8... done
Generation complete.

Then we load german locale locale.setlocale(locale.LC_ALL, 'german') and the same code as at the beginning (remember about importing again packages) gives us:

Loading avaliable locales via real names...
success: real_name =  C
success: real_name =  en_US.UTF-8
success: real_name =  de_DE.ISO8859-1
Loading avaliable locales via aliases...
success: alias =  deutsch  , real_name =  de_DE.ISO8859-1
success: alias =  german  , real_name =  de_DE.ISO8859-1

and the default formatting is more German:

local_format_str on Google Colab:  %a %d %b %Y %T %Z
now in Google Colab default format: Mi 01 Dez 2021 12:12:03
Related