From 9b2e290336e7c8de7117e1fb24ba5443fed56698 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Wed, 11 May 2016 11:40:47 +0300 Subject: Working locale --- CMakeLists.txt | 10 +++++++- categories.c | 23 ++++++++++++++++++ categories.h | 11 +++++++++ cmake/TranslationsMusl.cmake | 16 ++++++------ config.h.in | 1 + locale.c | 58 ++++++++++++++++++++++++++------------------ locale.in | 2 ++ po/CMakeLists.txt | 10 +++++--- 8 files changed, 95 insertions(+), 36 deletions(-) create mode 100644 config.h.in create mode 100644 locale.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 7483948..8ada96e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,14 @@ project(locales C) cmake_minimum_required(VERSION 2.8) +include(GNUInstallDirs) list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) -set(LOCALE_SOURCES locale.c categories.c categories.h) +configure_file(config.h.in config.h) +configure_file(locale.in 00locale.sh) +INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}) +set(GETTEXT_PACKAGE "musl-locales") +set(MUSL_LOCPATH ${CMAKE_INSTALL_DATAROOTDIR}/i18n/locales/musl CACHE PATH "Locales directory" FORCE) +set(LOCALE_SOURCES locale.c categories.c categories.h config.h) add_executable(locale ${LOCALE_SOURCES}) +install(PROGRAMS 00locale.sh DESTINATION "${CMAKE_INSTALL_SYSCONFDIR}/profile.d") +install(TARGETS locale DESTINATION bin) add_subdirectory(po) diff --git a/categories.c b/categories.c index 8d8ae9e..29a9b57 100644 --- a/categories.c +++ b/categories.c @@ -1,3 +1,13 @@ +/* + * + * Copyright (c) 2016 Konstantin Pugin + * Konstantin Pugin (ria.freelander@gmail.com) + * + * Licensed under the LGPL v3. + * + * A 'locale' command implementation for musl. + * + */ #include "categories.h" #include #include @@ -79,6 +89,19 @@ const struct cat_item get_cat_item_for_name(const char *name) return invalid; } +const char* get_cat_name_for_item_name(const char *name) +{ + for(int i = 0; i < LC_ALL; i++) + { + for(int j = 0; cats[i][j].type != CAT_TYPE_END; j++) + { + if(!strcmp(name,cats[i][j].name)) + return get_name_for_cat(i); + } + } + return ""; +} + const struct cat_item* get_cat_for_locale_cat(int locale_cat) { return cats[locale_cat]; diff --git a/categories.h b/categories.h index 9a5bec0..a027020 100644 --- a/categories.h +++ b/categories.h @@ -1,3 +1,13 @@ +/* + * + * Copyright (c) 2016 Konstantin Pugin + * Konstantin Pugin (ria.freelander@gmail.com) + * + * Licensed under the LGPL v3. + * + * A 'locale' command implementation for musl. + * + */ #ifndef CATEGORIES_H #define CATEGORIES_H @@ -29,4 +39,5 @@ const struct cat_item get_cat_item_for_name(const char* name); const struct cat_item* get_cat_for_locale_cat(int locale_cat); int get_cat_num_for_name(const char *cat_name); const char* get_name_for_cat(int cat_no); +const char* get_cat_name_for_item_name(const char *name); #endif // CATEGORIES_H diff --git a/cmake/TranslationsMusl.cmake b/cmake/TranslationsMusl.cmake index d944b70..4f12557 100644 --- a/cmake/TranslationsMusl.cmake +++ b/cmake/TranslationsMusl.cmake @@ -1,7 +1,7 @@ # Translations.cmake, CMake macros written for Marlin, feel free to re-use them -macro (add_translations_directory NLS_PACKAGE LOCPATH) - add_custom_target (i18n ALL COMMENT ?Building i18n messages.?) +macro (add_musl_translations_directory NLS_PACKAGE LOCPATH) + add_custom_target (musl-i18n ALL COMMENT ?Building i18n messages for C library.?) find_program (MSGFMT_EXECUTABLE msgfmt) # be sure that all languages are present # Using all usual languages code from https://www.gnu.org/software/gettext/manual/html_node/Language-Codes.html#Language-Codes @@ -20,12 +20,12 @@ macro (add_translations_directory NLS_PACKAGE LOCPATH) set (MO_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PO_INPUT_BASE}.mo) set (PO_COPY ${CMAKE_CURRENT_BINARY_DIR}/${PO_INPUT_BASE}.po) file (COPY ${PO_INPUT} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - add_custom_command (TARGET i18n COMMAND ${MSGFMT_EXECUTABLE} -o ${MO_OUTPUT} ${PO_INPUT}) + add_custom_command (TARGET musl-i18n COMMAND ${MSGFMT_EXECUTABLE} -o ${MO_OUTPUT} ${PO_INPUT}) install (FILES ${MO_OUTPUT} DESTINATION ${LOCPATH}) endforeach (PO_INPUT ${PO_FILES}) -endmacro (add_translations_directory) +endmacro (add_musl_translations_directory) # Apply the right default template. macro (create_po_file LANGUAGE_NEEDED) @@ -89,8 +89,8 @@ macro (create_po_file LANGUAGE_NEEDED) endif () endmacro (create_po_file) -macro (add_translations_catalog NLS_PACKAGE) - add_custom_target (pot COMMENT ?Building translation catalog.?) +macro (add_musl_translations_catalog NLS_PACKAGE) + add_custom_target (musl-pot COMMENT ?Building translation catalog for C library.?) find_program (XGETTEXT_EXECUTABLE xgettext) set(C_SOURCE "") @@ -112,7 +112,7 @@ macro (add_translations_catalog NLS_PACKAGE) set(CONTINUE_FLAG "") IF(NOT "${C_SOURCE}" STREQUAL "") - add_custom_command(TARGET pot WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND ${BASE_XGETTEXT_COMMAND} ${C_SOURCE}) + add_custom_command(TARGET musl-pot WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMMAND ${BASE_XGETTEXT_COMMAND} ${C_SOURCE}) set(CONTINUE_FLAG "-j") ENDIF() -endmacro () +endmacro (add_musl_translations_catalog) diff --git a/config.h.in b/config.h.in new file mode 100644 index 0000000..63ec30d --- /dev/null +++ b/config.h.in @@ -0,0 +1 @@ +#define PACKAGE "@GETTEXT_PACKAGE@" diff --git a/locale.c b/locale.c index 6079076..cfe2f20 100644 --- a/locale.c +++ b/locale.c @@ -1,12 +1,11 @@ -/* vi: set sw=4 ts=4: */ /* * - * Copyright (c) 2008 STMicroelectronics Ltd - * Filippo Arcidiacono (filippo.arcidiacono@st.com) + * Copyright (c) 2016 Konstantin Pugin + * Konstantin Pugin (ria.freelander@gmail.com) * - * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + * Licensed under the LGPL v3. * - * A 'locale' command implementation for uClibc. + * A 'locale' command implementation for musl. * */ @@ -22,6 +21,8 @@ #include #include #include +#include +#include "config.h" #include "categories.h" @@ -55,7 +56,6 @@ static void usage(char *name) "\t-c, --category-name\tWrite names of selected categories\n" "\t-k, --keyword-name\tWrite names of selected keywords\n" , s); - free(s); } static int argp_parse(int argc, char *argv[]) @@ -105,12 +105,15 @@ static int argp_parse(int argc, char *argv[]) static void list_locale() { const char *locpath = getenv("MUSL_LOCPATH"); - DIR *dir = opendir(locpath); - struct dirent *pDir; - printf("C"); - printf("C.UTF-8"); - while ((pDir = readdir(dir)) != NULL){ - printf("%s.%s\n",pDir->d_name,"UTF-8"); + printf("C\n"); + printf("C.UTF-8\n"); + if(locpath != NULL) + { + DIR *dir = opendir(locpath); + struct dirent *pDir; + while ((pDir = readdir(dir)) != NULL){ + printf("%s.%s\n",pDir->d_name,"utf8"); + } } } @@ -139,12 +142,13 @@ static void print_item (struct cat_item item) if (show_keyword_name) printf ("%s=\"", item.name); - for (int cnt = item.min; cnt <= item.max; ++cnt) + for (int cnt = item.min; cnt <= item.max; cnt++) { - val = nl_langinfo (item.id); + val = nl_langinfo (cnt); if (val != NULL) fputs (val, stdout); - putchar (';'); + if (cnt < item.max) + putchar (';'); } if (show_keyword_name) putchar ('"'); @@ -157,7 +161,7 @@ static void print_item (struct cat_item item) /* Show the information request for NAME. */ static void show_info(const char *name) { - for (size_t cat_no = 0; cat_no < LC_ALL; ++cat_no) + for (size_t cat_no = 0; cat_no < LC_ALL; cat_no++) { if (strcmp (name, get_name_for_cat(cat_no)) == 0) /* Print the whole category. */ @@ -169,14 +173,10 @@ static void show_info(const char *name) print_item(items[j]); return; } - else - { - if (show_category_name != 0) - puts (get_name_for_cat(cat_no)); - print_item(get_cat_item_for_name(name)); - } - } + if (show_category_name != 0) + puts (get_cat_name_for_item_name(name)); + print_item(get_cat_item_for_name(name)); } static void show_locale_vars() @@ -198,12 +198,20 @@ static void show_locale_vars() int main(int argc, char *argv[]) { + if (setlocale (LC_CTYPE, "") == NULL) + fprintf (stderr, gettext ("Cannot set LC_CTYPE to default locale")); + if (setlocale (LC_MESSAGES, "") == NULL) + fprintf (stderr, gettext ("Cannot set LC_MESSAGES to default locale")); /* Parse and process arguments. */ + textdomain (PACKAGE); if (argp_parse(argc, argv)) return 1; if (do_all) { - list_locale(); + if (setlocale (LC_COLLATE, "") == NULL) + fprintf (stderr, gettext ("Cannot set LC_COLLATE to default locale")); + else + list_locale(); exit(EXIT_SUCCESS); } @@ -217,6 +225,8 @@ int main(int argc, char *argv[]) exit(EXIT_SUCCESS); } + if (setlocale (LC_ALL, "") == NULL) + fprintf (stderr, gettext ("Cannot set LC_ALL to default locale")); /* If no real argument is given we have to print the contents of the current locale definition variables. These are LANG and the LC_*. */ if (remaining == argc && show_category_name == 0 diff --git a/locale.in b/locale.in new file mode 100644 index 0000000..7c671cd --- /dev/null +++ b/locale.in @@ -0,0 +1,2 @@ +#!/bin/sh +export MUSL_LOCPATH="@CMAKE_INSTALL_FULL_DATAROOTDIR@/@MUSL_LOCPATH@" diff --git a/po/CMakeLists.txt b/po/CMakeLists.txt index 606b49a..90716a0 100644 --- a/po/CMakeLists.txt +++ b/po/CMakeLists.txt @@ -1,4 +1,8 @@ include(TranslationsMusl) -add_translations_directory("musl" MUSL_LOCPATH) -add_translations_catalog("musl" - ../musl) +add_musl_translations_directory("musl" MUSL_LOCPATH) +add_musl_translations_catalog("musl" + ../musl) +include(Translations) +add_translations_directory("musl-locales" "bin") +add_translations_catalog("musl-locales" + ..) -- cgit v1.2.3-70-g09d2