-
Notifications
You must be signed in to change notification settings - Fork 0
/
managing-locale-with-linux-and-python.html
114 lines (91 loc) · 5.33 KB
/
managing-locale-with-linux-and-python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ## for client-side less
<link rel="stylesheet/less" type="text/css" href="http://thierry.ducrest.net/theme/css/style.less">
<script src="http://cdnjs.cloudflare.com/ajax/libs/less.js/1.7.3/less.min.js" type="text/javascript"></script>
-->
<link rel="stylesheet" href="http://thierry.ducrest.net/theme/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="http://thierry.ducrest.net/theme/css/style.css">
<link rel="stylesheet" type="text/css" href="http://thierry.ducrest.net/theme/css/pygments.css">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=PT+Sans|PT+Serif|PT+Mono">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Thierry Ducrest">
<meta name="description" content="Posts and writings by Thierry Ducrest">
<link href="http://feeds.thierry.ducrest.net/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title=" Atom" />
<meta name="keywords" content="Linux, Python">
<title>
– Managing locale with Linux and Python </title>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-41998474-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<aside>
<div id="user_meta">
<a href="http://thierry.ducrest.net">
<img src="http://thierry.ducrest.net/theme/images/empty.png" alt="logo">
</a>
<h2><a href="http://thierry.ducrest.net">Thierry Ducrest</a></h2>
<p>Software Developer</p>
<ul>
<li><a href="https://github.com/TDu" target="_blank"><i class="fa fa-github fa-lg"></i></a></li>
<li><a href="https://cl.linkedin.com/in/thierry-ducrest-928a394" target="_blank"><i class="fa fa-linkedin-square fa-lg"></i></a></li>
<li><a href="https://twitter.com/thducrest" target="_blank"><i class="fa fa-twitter fa-lg"></i></a></li>
</ul>
</div>
</aside>
<main>
<header>
<p>
<a href="http://thierry.ducrest.net">Index</a> ¦ <a href="http://thierry.ducrest.net/archives.html">Archives</a>
¦ <a href="http://feeds.thierry.ducrest.net/feeds/all.atom.xml">Atom</a>
</p>
</header>
<article>
<h3><a href="http://thierry.ducrest.net/managing-locale-with-linux-and-python.html">Managing locale with Linux and Python</a></h3>
<span class="date-article">20 September 2016</span>
<div class="article_text">
<p>As far as I can remember I have always installed my operating systems to use the english language, maybe because I did not have the luxury of the choice before and it kind of feels more natural now.</p>
<p>Lately I have had to print names (month, days) of the calendar in french using Python, so I dig a little in "locale", which are the set of parameters that configure the language and region settings used by the system.</p>
<p>On Debian to get the list of "locale" installed</p>
<div class="highlight"><pre><span></span>locale -a
</pre></div>
<p>To choose which "locale" to make available on the system you can manually edit the file locale.gen by removing the # in front of the line you want activated, followed by the command to generate the modification :</p>
<div class="highlight"><pre><span></span>sudo vim /etc/locale.gen
locale-gen
</pre></div>
<p>Or simply run this configuration command :</p>
<div class="highlight"><pre><span></span>sudo dpkg-reconfigure locales
</pre></div>
<p>Now that the correct "locale" is installed on the system, it is possible to display a date with the months and days in the desired language :</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">locale</span>
<span class="kn">import</span> <span class="nn">datetime.date</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
<span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_ALL</span><span class="p">,</span> <span class="s1">'fr_CH.utf8'</span><span class="p">)</span>
<span class="n">d</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">'</span><span class="si">%d</span><span class="s1"> %B %Y'</span><span class="p">)</span>
</pre></div>
</div>
<div class="article_meta">
<p>Posted on: Tue 20 September 2016</p>
<p>Category: <a href="http://thierry.ducrest.net/category/it.html">IT</a>
– Tags:
<a href="http://thierry.ducrest.net/tag/linux.html">Linux</a>, <a href="http://thierry.ducrest.net/tag/python.html">Python</a> </p>
</div>
</article>
<div id="ending_message">
<!--
<p>© Thierry Ducrest. Built using <a href="http://getpelican.com" target="_blank">Pelican</a>.</p>
-->
</div>
</main>
</body>
</html>