新增国际化模块

This commit is contained in:
shaozhimin 2024-09-11 18:34:12 +08:00
parent ed72f6d833
commit 242ceb6711
11 changed files with 377 additions and 0 deletions

9
debian/changelog vendored
View File

@ -1,3 +1,12 @@
libkysdk-system (2.5.1.2-0k0.13) nile; urgency=medium
* BUG
* 需求号:无
* 其他改动说明:新增国际化模块
* 其他改动影响域:无
-- szm-min <shaozhimin@kylinos.cn> Wed, 11 Sep 2024 18:58:46 +0800
libkysdk-system (2.5.1.2-0k0.12) nile; urgency=medium
* BUG

16
debian/control vendored
View File

@ -544,3 +544,19 @@ Section: utils
Depends: libkysdk-storage (>= ${binary:Version})
Multi-Arch: foreign
Description: 存储模块库 - 开发库
Package: libkysdk-global
Architecture: any
Depends: ${misc:Depends},
${shlibs:Depends},
libc6,
libcjson1,
libkysdk-location
Description: 国际化模块
Package: libkysdk-global-dev
Architecture: all
Section: utils
Depends: libkysdk-global (>= ${binary:Version})
Multi-Arch: foreign
Description: 国际化模块 - 开发库

2
debian/libkysdk-global-dev.install vendored Normal file
View File

@ -0,0 +1,2 @@
usr/include/kysdk/kysdk-system/libkyglobal.h
development-files/kysdk-global.pc usr/share/pkgconfig/

1
debian/libkysdk-global.install vendored Normal file
View File

@ -0,0 +1 @@
usr/lib/*/libkyglobal.so*

8
debian/libkysdk-global.symbols vendored Normal file
View File

@ -0,0 +1,8 @@
# SymbolsHelper-Confirmed: 2.5.1.2 arm64
libkyglobal.so.1 libkysdk-global #MINVER#
kdk_free_langlist@Base 2.5.1.2
kdk_global_get_raw_offset@Base 2.5.1.2
kdk_global_get_region_match_language@Base 2.5.1.2
kdk_global_get_rtl@Base 2.5.1.2
kdk_global_get_system_language@Base 2.5.1.2
kdk_global_get_system_support_language@Base 2.5.1.2

View File

@ -0,0 +1,6 @@
Name: libkysdk-global
Description: kysdk system layer global component
Requires:
Version: 2.5.1
Libs: -lkyglobal
Cflags: -I/usr/include/kysdk/kysdk-system/

View File

@ -5,6 +5,11 @@
# COMMAND sh ${PROJECT_SOURCE_DIR}/src/realtime/smartctl/configure
# WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/realtime/smartctl)
include_directories(location)
include_directories(systeminfo)
include_directories(global)
add_subdirectory(disk)
add_subdirectory(systeminfo)
add_subdirectory(systemtime)
@ -25,3 +30,4 @@ add_subdirectory(image)
add_subdirectory(battery)
add_subdirectory(accounts)
add_subdirectory(storage)
add_subdirectory(global)

29
src/global/CMakeLists.txt Normal file
View File

@ -0,0 +1,29 @@
aux_source_directory(. SOURCESCODE)
include_directories(.)
add_library(kyglobal SHARED ${SOURCESCODE})
set_target_properties(kyglobal PROPERTIES VERSION 2.5.1 SOVERSION 1)
add_executable(kyglobal-test test/kyglobal-test.c)
target_link_libraries(kyglobal kylocation cjson)
target_link_libraries(kyglobal-test kyglobal kysysinfo)
# GNU
include(GNUInstallDirs)
# CMAKE_INSTALL_LIBDIR
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "armv8l")
set(CMAKE_INSTALL_LIBDIR "/usr/lib/arm-linux-gnueabihf")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "mips64")
set(CMAKE_INSTALL_LIBDIR "/usr/lib/mips64el-linux-gnuabi64")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i686")
set(CMAKE_INSTALL_LIBDIR "/usr/lib/i386-linux-gnu")
else()
set(CMAKE_INSTALL_LIBDIR "/usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu")
endif()
#
install(TARGETS kyglobal
DESTINATION ${CMAKE_INSTALL_LIBDIR})
# install(TARGETS kyglobal
# DESTINATION lib/kysdk/kysdk-system)
install(FILES libkyglobal.h
DESTINATION include/kysdk/kysdk-system)

191
src/global/libkyglobal.c Normal file
View File

@ -0,0 +1,191 @@
#include "libkyglobal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kysdk/kysdk-base/cstring-extension.h>
#include <unistd.h>
#include <libkylocation.h>
#include <cjson/cJSON.h>
#include <time.h>
#define KDK_LANGUAGE_FILE "/usr/share/i18n/SUPPORTED"
#define KDK_SYSTEM_LANGUAGE "LANG"
char **kdk_global_get_system_support_language()
{
FILE *fp = NULL;
char line[128] = "\0";
fp = fopen(KDK_LANGUAGE_FILE, "rt");
if (!fp)
return NULL;
char **language = NULL;
char **tmp = NULL;
int index = 0;
while(fgets(line, sizeof(line), fp))
{
tmp = (char **)realloc(language, sizeof(char *) * (index + 2));
if(!tmp)
{
goto err_out;
}
language = tmp;
language[index] = (char *)malloc(sizeof(line));
if(!language[index])
{
goto err_out;
}
strstripspace(line);
strcpy(language[index], line);
index++;
}
fclose(fp);
language[index] = NULL;
return language;
err_out:
while (index)
{
free(language[index - 1]);
index--;
}
free(language);
fclose(fp);
return NULL;
}
bool kdk_global_get_region_match_language()
{
char area[12] = "\0";
char * lang = getenv(KDK_SYSTEM_LANGUAGE);
sscanf(lang, "%*[^_]_%2s", area);
char *location = kdk_loaction_get();
cJSON *json = cJSON_Parse(location);
if (json == NULL) {
return false;
}
// 获取 outputs 数组
cJSON *outputs = cJSON_GetObjectItem(json, "address");
if (strlen(outputs->valuestring) != 0)
{
char **fg = strsplit(outputs->valuestring, '|');
if (!fg)
{
return false;
}
if(strcmp(area, fg[0]) == 0)
{
free(fg);
return true;
}
else if(strcmp(area, "TW") == 0 && strcmp(fg[0], "CN") == 0)
{
free(fg);
return true;
}
else if(strcmp(area, "HK") == 0 && strcmp(fg[0], "CN") == 0)
{
free(fg);
return true;
}
else if(strcmp(area, "MO") == 0 && strcmp(fg[0], "CN") == 0)
{
free(fg);
return true;
}
else
{
free(fg);
return false;
}
}
return false;
}
bool kdk_global_get_rtl()
{
char *language[] = {"ar_AE", "ar_BH", "ar_DZ", "ar_EG", "ar_IN", "ar_IQ", "ar_JO", "ar_KW", "ar_LB", "ar_LY", "ar_MA", "ar_OM", "ar_QA",
"ar_SA", "ar_SD", "ar_SS", "ar_SY", "ar_TN", "ar_YE", "fa_IR", "ug_CN", "he_IL", "ur_IN", "ur_PK", "kk_KZ", "ky_KG"};
char *lang = getenv(KDK_SYSTEM_LANGUAGE);
int size = (sizeof(language) / sizeof(language[0]));
// int i = 0;
for(int i = 0; i < size; i++)
{
if(strstr(lang, language[i]))
{
return true;
}
}
return false;
}
int kdk_global_get_raw_offset()
{
time_t t;
struct tm *local_tm;
char buffer[10] = "\0"; // 用于存储格式化后的时区字符串
char buff[10] = "\0";
// 获取当前时间
time(&t);
// 获取本地时间
local_tm = localtime(&t);
// 使用 strftime 格式化时区偏移量,格式为 ±hh:mm
strftime(buffer, sizeof(buffer), "%z", local_tm);
sprintf(buff, "%.3s", buffer);
int offset = atoi(buff);
return offset;
}
char* kdk_global_get_system_language(const char *username)
{
if(!username)
return NULL;
char lang[128] = "\0";
char *language = NULL;
char path[128] = "\0";
char line[512] = "\0";
FILE *fp = NULL;
sprintf(path, "/home/%s/.pam_environment", username);
fp = fopen(path, "r");
if(!fp)
return NULL;
while (fgets(line, sizeof(line), fp))
{
sscanf(line, "%s", lang);
if(strcmp(lang, "LANG") == 0)
{
language = (char*)malloc(sizeof(char) * 24);
if(!language)
{
fclose(fp);
return NULL;
}
sscanf(line, "%*[^=]=%[a-zA-Z_]", language);
break;
}
}
fclose(fp);
return language;
}
void kdk_free_langlist(char** langlist)
{
if (! langlist)
return;
size_t index = 0;
while (langlist[index])
{
free(langlist[index]);
index ++;
}
free(langlist);
}

76
src/global/libkyglobal.h Normal file
View File

@ -0,0 +1,76 @@
#ifndef LIBKYGLOBAL_H
#define LIBKYGLOBAL_H
/**
* @file libkyglobal.h
* @author liuyunhe (liuyunhe@kylinos.cn)
* @brief
* @version 0.1
* @date 2024-2-20
*
* @copyright Copyright (c) 2021
* @defgroup libkysdk-global
* @{
*
*/
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief
*
* @return char**
*/
extern char** kdk_global_get_system_support_language();
/**
* @brief
*
* @return bool -true-false
*/
extern bool kdk_global_get_region_match_language();
/**
* @brief
*
* @return bool -true-false
*/
extern bool kdk_global_get_rtl();
/**
* @brief UTC时区的偏差
*
* @return int = 8西 = -2
*/
extern int kdk_global_get_raw_offset();
/**
* @brief
*
* @param username
* @return char* :"zh_CN":"en_CN":"bo_CN" free NULL
*/
extern char* kdk_global_get_system_language(const char *username);
/**
* @brief kdk_global_get_system_support_language返回的系统支持的语言列表
*
* @param langlist kdk_global_get_system_support_language返回的字符串指针
*/
extern void kdk_free_langlist(char** langlist);
#ifdef __cplusplus
}
#endif
#endif
/**
* \example kysdk-system/src/battery/test/kybattery-test.c
*
*/

View File

@ -0,0 +1,33 @@
#include "stdio.h"
#include "../libkyglobal.h"
#include <stdlib.h>
#include <libkysysinfo.h>
int main()
{
int index = 0;
char **language = kdk_global_get_system_support_language();
if (language)
{
while (language[index])
{
printf("index = %d, language = %s\n", index, language[index]);
index++;
}
kdk_free_langlist(language);
}
bool match = kdk_global_get_region_match_language();
printf("match = %d\n", match);
bool rtl = kdk_global_get_rtl();
printf("rtl = %d\n", rtl);
int offset = kdk_global_get_raw_offset();
printf("offset = %d\n", offset);
char *eUser = kdk_system_get_eUser();
char* system_language = kdk_global_get_system_language(eUser);
printf("system_language = %s\n", system_language);
return 0;
}