Set locale to system default UTF-8
Asked Answered
B

3

30

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?

Brandebrandea answered 13/12, 2013 at 23:30 Comment(3)
Have you considered setting the encoding to 'UTF-8' on any reads from the "outside"?Hypotension
I'm not sure what you meanBrandebrandea
I was misremembering something I read earlier this week on rdev. Perhaps one of these will help: #9388410 #17985701 #5675019Hypotension
B
5

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.

Brandebrandea answered 24/6, 2017 at 14:14 Comment(1)
Thank you, that indeed solved the issue with RApache when sending text to the webservice in UTF-8Adoree
A
2

Try this:

Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")
Automate answered 8/4, 2014 at 4:28 Comment(4)
Yes, that is correct. I tested it only on Windows 7, 64 bit. I am not sure about linux versions, but I remember once I had this issue on ubuntu, and I followed the instructions on this link to change my locale settings. askubuntu.com/questions/162391/how-do-i-fix-my-locale-issue. Please do not quote me for linux versions, because I am not sure.Automate
I just tested this on my ubuntu machine. I followed the link in my previous comment. Then, I opened a fresh "R" session. The locale settings were automatically changed to en_US.UTF8. If you want to revert back to "C" locale type or "POSIX" type, use the function: Sys.setlocale(category = "LC_ALL", locale = "C"). HTHAutomate
This does not actually set the locale to UTF-8. This setting is latin1.Walkthrough
On my case, i solved my encoding issues by using the same line but setting "en_US.UTF-8" as locale. Many thanks for thisAllelomorph
H
1

I guess you need to make a check for the OS. The locale names differ by OS, see the examples at the help file.

?Sys.getlocale()

Examples

Sys.getlocale()
Sys.getlocale("LC_TIME")
## Not run: 
Sys.setlocale("LC_TIME", "de")     # Solaris: details are OS-dependent
Sys.setlocale("LC_TIME", "de_DE.utf8")   # Modern Linux etc.
Sys.setlocale("LC_TIME", "de_DE.UTF-8")  # ditto
Sys.setlocale("LC_TIME", "de_DE")  # OS X, in UTF-8
Sys.setlocale("LC_TIME", "German") # Windows

## End(Not run)
Sys.getlocale("LC_PAPER")          # may or may not be set

## Not run: 
Sys.setlocale("LC_COLLATE", "C")   # turn off locale-specific sorting,
                                   # usually, but not on all platforms
## End(Not run)
Heliochrome answered 3/11, 2015 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.