Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set locale to system default UTF-8

When running R inside rApache, the locale is inherited from the Apache webserver, and therefore Sys.getlocale() is always equal to "C". I would like my web application to use UTF8, so I use:

Sys.setlocale("LC_ALL", 'en_US.UTF-8') 

However this doesn't work on machines that do not have this locale available:

1: Setting LC_CTYPE failed, using "C"  2: Setting LC_COLLATE failed, using "C"  3: Setting LC_TIME failed, using "C"  4: Setting LC_MESSAGES failed, using "C"  5: Setting LC_MONETARY failed, using “C” 

Is there any way to use Sys.setlocale to set the locale to the system default UTF-8? I.e. something that would also work on Windows or a German Linux?

like image 975
Jeroen Ooms Avatar asked Dec 13 '13 23:12

Jeroen Ooms


People also ask

What is locale UTF-8?

UTF-8 locale is a significant Unicode locale in the Solaris 8 product. It supports and provides multiscript processing capability by using UTF-8 as its codeset. It can input and output text in multiple scripts. This was the first locale with this capability in the Solaris operating environment.

What is default system locale?

SystemLocale specifies the default language to use for non-Unicode programs. This setting is used by both Windows Setup and Windows Deployment Services. The system locale specifies which bitmap fonts and code pages (for example, ANSI or DOS) are used on the system by default.


1 Answers

Answering my own question: On Ubuntu the default LANG is defined in /etc/default/locale:

jeroen@dev:~⟫ cat /etc/default/locale # Created by cloud-init v. 0.7.7 on Wed, 29 Jun 2016 11:02:51 +0000 LANG="en_US.UTF-8" 

So in R we could do something like:

readRenviron("/etc/default/locale") LANG <- Sys.getenv("LANG") if(nchar(LANG))    Sys.setlocale("LC_ALL", LANG) 

Apache also has a line in /etc/apache2/envvars that can be uncommented to enable this.

like image 186
Jeroen Ooms Avatar answered Oct 03 '22 02:10

Jeroen Ooms