LINUX系统编程 LINUX地区(locale)设置

简介: LINUX本地的locale设置使用/etc/sysconfig/i18n进行设置, 这里的i18n是术语internationalization的缩写i加上18字符 在加上n,这样更为方便,同时区一样LINUX地区系统维护在 /usr/share/locale...
LINUX本地的locale设置使用/etc/sysconfig/i18n进行设置,
这里的i18n是术语internationalization的缩写i加上18字符
在加上n,这样更为方便,同时区一样LINUX地区系统维护在
/usr/share/locale中,该目录下每个目录都是特定地区信息
的集合,目录的约定为:
language(ISO语言代码)[_territory(ISO国家代码)[.codeset(字符集)]][@modifier(如果前面都一样这个用于区分)]
一般只包含language(语言)_territory(国家)


如: 
zh_CN:中文-中国
en_US:英文-美国
fr_CH:瑞士-法语 


而每一个目录包含了具体的规则如:
LC_CTYPE:字符分类,大小写转换
LC_COLLATE:字符集排序规则
LC_MONETARY:对币值的格式化规则
LC_NUMERIC:对比值以外数据的格式化隔着
LC_TIME:时间日志的规则
LC_MESSAGES:
我们可以locale 查看这些信息,如果设置i18n为一个不存在的目录那么将会报错
[root@testmy locale]# more /etc/sysconfig/i18n 
LANG="123.UTF-8"
[root@testmy locale]# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory


这里我们注意一下:
LANG
LC_ALL
LC_*
i18n配置文件
的区别


i18n<LANG<LC_* 
也就是LANG会比i18n优先级高,LC_*比LANG高,而LC_ALL实际就是一次设置全部的LC_*而已
但是一旦设置了LC_ALL就不能设置LC_*了,所以一般不用。
一般我们使用LANG来设置全部的LC_*,而独立设置一个LC_*为想要的设置
比如我这里设置LANG=en_US.UTF-8 ,设置LC_TIME=zh_GN.UTF-8
目的在于输出时间以中文形式输出
[root@testmy ~]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
[root@testmy ~]# date
Wed Dec  7 09:02:28 CST 2016
[root@testmy ~]# export LC_TIME=zh_CN.UTF-8
[root@testmy ~]# date
2016年 12月 07日 星期三 09:03:23 CST


使用程序验证LANG的优先级低于LC_TIME
[root@testmy ~]# LANG=zh_CN.UTF-8 LC_TIME=en_US.UTF-8 ./a.out 
ctime() is:Wed Dec  7 10:49:32 2016
asmtime is:Wed Dec  7 10:49:32 2016
stftime() is:Wednesday 2016-12-07 10:49:32 CST
[root@testmy ~]# LANG=en_US.UTF-8 LC_TIME=zh_CN.UTF-8  ./a.out 
ctime() is:Wed Dec  7 10:49:35 2016
asmtime is:Wed Dec  7 10:49:35 2016
stftime() is:星期三 2016-12-07 10:49:35 CST

代码:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include <time.h>
  3. #include <errno.h>
  4. #include <locale.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>


  7. int main(void)
  8. {
  9.         time_t t;
  10.         struct tm *loc;
  11.         char* ctc;
  12.         char* asc;
  13.         char ut[200];

  14.         t = time(NULL);
  15.         if(setlocale(LC_ALL,"") == NULL) //LC_ALL,"" will use env vars
  16.         {
  17.                 perror("setlocale:");
  18.                 exit(1);
  19.         }
  20.         ctc = ctime(&t);
  21.         if ((loc = localtime(&t)) ==NULL)
  22.         {
  23.                 perror("localtime:");
  24.                 exit(2);
  25.         }

  26.         asc = asctime(loc);

  27.         if(strftime(ut,200,"%A %F %T %Z\n",loc) == 0)
  28.         {
  29.                 perror("strftime:");
  30.                 exit(3);
  31.         }

  32.         printf("ctime() is:%sasmtime is:%sstftime() is:%s",ctc,asc,ut);
  33.         return 0;

  34. }

相关文章
|
5天前
|
Ubuntu 安全 Linux
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
《Linux 简易速速上手小册》第1章: Linux 系统基础(2024 最新版)
37 1
|
12天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
52 2
|
1天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
32 2
|
1天前
|
缓存 Linux
linux系统缓存机制
linux系统缓存机制
|
1天前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
14 0
|
1天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
3 0
|
1天前
|
Ubuntu Linux
Linux(22) Linux设置网络优先级顺序
Linux(22) Linux设置网络优先级顺序
3 0
|
3天前
|
资源调度 JavaScript Ubuntu
Linux系统之部署briefing视频聊天系统
【4月更文挑战第21天】Linux系统之部署briefing视频聊天系统
37 2
|
4天前
|
Linux Perl
Linux系统替换字符串常用命令
请注意,`sed`命令可以非常强大,可以根据不同的需求使用不同的选项和正则表达式来进行更复杂的字符串替换操作。
16 0
|
8天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令