Skip to content

Commit

Permalink
Working locale
Browse files Browse the repository at this point in the history
  • Loading branch information
rilian-la-te committed May 11, 2016
1 parent a24e4fa commit 9b2e290
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 36 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions categories.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
*
* Copyright (c) 2016 Konstantin Pugin
* Konstantin Pugin ([email protected])
*
* Licensed under the LGPL v3.
*
* A 'locale' command implementation for musl.
*
*/
#include "categories.h"
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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];
Expand Down
11 changes: 11 additions & 0 deletions categories.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
*
* Copyright (c) 2016 Konstantin Pugin
* Konstantin Pugin ([email protected])
*
* Licensed under the LGPL v3.
*
* A 'locale' command implementation for musl.
*
*/
#ifndef CATEGORIES_H
#define CATEGORIES_H

Expand Down Expand Up @@ -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
16 changes: 8 additions & 8 deletions cmake/TranslationsMusl.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 "")
Expand All @@ -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)
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define PACKAGE "@GETTEXT_PACKAGE@"
58 changes: 34 additions & 24 deletions locale.c
Original file line number Diff line number Diff line change
@@ -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.
*
*/

Expand All @@ -22,6 +21,8 @@
#include <dirent.h>
#include <locale.h>
#include <stdbool.h>
#include <libintl.h>
#include "config.h"
#include "categories.h"


Expand Down Expand Up @@ -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[])
Expand Down Expand Up @@ -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");
}
}
}

Expand Down Expand Up @@ -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 ('"');
Expand All @@ -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. */
Expand All @@ -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()
Expand All @@ -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);
}

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions locale.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
export MUSL_LOCPATH="@CMAKE_INSTALL_FULL_DATAROOTDIR@/@MUSL_LOCPATH@"
10 changes: 7 additions & 3 deletions po/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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"
..)

0 comments on commit 9b2e290

Please sign in to comment.