Skip to content

Commit 0704a4b

Browse files
locales init
Signed-off-by: Nicolas Adenis-Lamarre <nicolas.adenis.lamarre@gmail.com>
1 parent 9d793db commit 0704a4b

File tree

11 files changed

+165
-6
lines changed

11 files changed

+165
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.slo
33
*.lo
44
*.o
5+
*.mo
56

67
# Compiled Dynamic libraries
78
*.so

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ else()
235235
endif()
236236
endif()
237237

238+
# i18n
239+
find_program (MSGFMT_EXECUTABLE msgfmt)
240+
find_program (MSGMERGE_EXECUTABLE msgmerge)
241+
find_program (XGETTEXT_EXECUTABLE xgettext)
242+
if(MSGFMT_EXECUTABLE AND MSGMERGE_EXECUTABLE AND XGETTEXT_EXECUTABLE)
243+
message (STATUS "Native language support enabled.")
244+
add_definitions(-DHAVE_GETTEXT)
245+
add_subdirectory (locale)
246+
endif()
247+
add_definitions(-DINSTALLATION_PREFIX_DIR=\"${CMAKE_INSTALL_PREFIX}\")
248+
238249
#-------------------------------------------------------------------------------
239250
# set up build directories
240251
set(dir ${CMAKE_CURRENT_SOURCE_DIR})

es-app/src/main.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#ifdef WIN32
2525
#include <Windows.h>
2626
#endif
27+
#include "LocaleES.h"
2728

2829
#include <FreeImage.h>
2930

@@ -196,6 +197,28 @@ bool verifyHomeFolderExists()
196197
return true;
197198
}
198199

200+
void locales_init() {
201+
#ifdef HAVE_GETTEXT
202+
char* btd;
203+
char* cs;
204+
205+
#define LOCALEDIR INSTALLATION_PREFIX_DIR "/share/locale"
206+
// local dir ; uncomment if you want to use locally compiled .mo files instead of .mo from the installation dir (present only after make install)
207+
//#define LOCALEDIR "./locale/lang"
208+
209+
setlocale (LC_MESSAGES, "");
210+
textdomain("emulationstation");
211+
if((btd=bindtextdomain("emulationstation", LOCALEDIR)) == NULL) {
212+
return;
213+
}
214+
215+
cs = bind_textdomain_codeset("emulationstation", "UTF-8");
216+
if(cs == NULL) {
217+
/* outch not enough memory, no real thing to do */
218+
}
219+
#endif
220+
}
221+
199222
// Returns true if everything is OK,
200223
bool loadSystemConfigFile(const char** errorString)
201224
{
@@ -287,6 +310,9 @@ int main(int argc, char* argv[])
287310
//always close the log on exit
288311
atexit(&onExit);
289312

313+
// Set locale
314+
locales_init();
315+
290316
Window window;
291317
SystemScreenSaver screensaver(&window);
292318
PowerSaver::init();
@@ -311,9 +337,9 @@ int main(int argc, char* argv[])
311337
LOG(LogInfo) << " ARB_texture_non_power_of_two: " << (glExts.find("ARB_texture_non_power_of_two") != std::string::npos ? "ok" : "MISSING");
312338
if(splashScreen)
313339
{
314-
std::string progressText = "Loading...";
340+
std::string progressText = _("Loading...");
315341
if (splashScreenProgress)
316-
progressText = "Loading system config...";
342+
progressText = _("Loading system config...");
317343
window.renderLoadingScreen(progressText);
318344
}
319345
}
@@ -333,7 +359,7 @@ int main(int argc, char* argv[])
333359
// we can't handle es_systems.cfg file problems inside ES itself, so display the error message then quit
334360
window.pushGui(new GuiMsgBox(&window,
335361
errorMsg,
336-
"QUIT", [] {
362+
_("QUIT"), [] {
337363
SDL_Event* quit = new SDL_Event();
338364
quit->type = SDL_QUIT;
339365
SDL_PushEvent(quit);
@@ -354,7 +380,7 @@ int main(int argc, char* argv[])
354380
ViewController::get()->preload();
355381

356382
if(splashScreen && splashScreenProgress)
357-
window.renderLoadingScreen("Done.");
383+
window.renderLoadingScreen(_("Done."));
358384

359385
//choose which GUI to open depending on if an input configuration already exists
360386
if(errorMsg == NULL)

es-app/src/views/SystemView.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "Settings.h"
1010
#include "SystemData.h"
1111
#include "Window.h"
12+
#include "LocaleES.h"
1213

1314
// buffer values for scrolling velocity (left, stopped, right)
1415
const int logoBuffersLeft[] = { -5, -2, -1 };
@@ -255,11 +256,14 @@ void SystemView::onCursorChanged(const CursorState& /*state*/)
255256
// also change the text after we've fully faded out
256257
setAnimation(infoFadeOut, 0, [this, gameCount] {
257258
std::stringstream ss;
259+
char strbuf[256];
258260

259261
if (!getSelected()->isGameSystem())
260262
ss << "CONFIGURATION";
261-
else
262-
ss << gameCount << " GAMES AVAILABLE";
263+
else {
264+
snprintf(strbuf, 256, ngettext("%i GAME AVAILABLE", "%i GAMES AVAILABLE", gameCount), gameCount);
265+
ss << strbuf;
266+
}
263267

264268
mSystemInfo.setText(ss.str());
265269
}, false, 1);

es-core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(CORE_HEADERS
1212
${CMAKE_CURRENT_SOURCE_DIR}/src/InputManager.h
1313
${CMAKE_CURRENT_SOURCE_DIR}/src/Log.h
1414
${CMAKE_CURRENT_SOURCE_DIR}/src/MameNames.h
15+
${CMAKE_CURRENT_SOURCE_DIR}/src/LocaleES.h
1516
${CMAKE_CURRENT_SOURCE_DIR}/src/platform.h
1617
${CMAKE_CURRENT_SOURCE_DIR}/src/PowerSaver.h
1718
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer.h
@@ -88,6 +89,7 @@ set(CORE_SOURCES
8889
${CMAKE_CURRENT_SOURCE_DIR}/src/InputManager.cpp
8990
${CMAKE_CURRENT_SOURCE_DIR}/src/Log.cpp
9091
${CMAKE_CURRENT_SOURCE_DIR}/src/MameNames.cpp
92+
${CMAKE_CURRENT_SOURCE_DIR}/src/LocaleES.cpp
9193
${CMAKE_CURRENT_SOURCE_DIR}/src/platform.cpp
9294
${CMAKE_CURRENT_SOURCE_DIR}/src/PowerSaver.cpp
9395
${CMAKE_CURRENT_SOURCE_DIR}/src/Renderer_draw_gl.cpp

es-core/src/LocaleES.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "LocaleES.h"
2+
3+
#ifndef HAVE_GETTEXT
4+
char* ngettext(char* msgid, char* msgid_plural, unsigned long int n) {
5+
if(n > 1) {
6+
return msgid_plural;
7+
}
8+
return msgid;
9+
}
10+
#endif

es-core/src/LocaleES.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _LOCALE_H_
2+
#define _LOCALE_H_
3+
4+
#ifdef HAVE_GETTEXT
5+
#include <libintl.h>
6+
#define _(A) gettext(A)
7+
#else
8+
#define _(A) A
9+
char* ngettext(char* msgid, char* msgid_plural, unsigned long int n);
10+
#endif
11+
12+
#endif

locale/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_custom_target (i18n ALL COMMENT "Building i18n messages.")
2+
file (GLOB es_PO_FILES ${CMAKE_CURRENT_SOURCE_DIR}/lang/*)
3+
set(es_POT ${CMAKE_CURRENT_SOURCE_DIR}/emulationstation.pot)
4+
# update the pot
5+
MESSAGE("Building the pot file")
6+
add_custom_command (TARGET i18n COMMAND find "${CMAKE_SOURCE_DIR}/es-app" "${CMAKE_SOURCE_DIR}/es-core" -name "*.cpp" -o -name "*.h" | ${XGETTEXT_EXECUTABLE} -f - -o ${es_POT} --no-location --keyword=_)
7+
8+
foreach (es_PO_INPUT ${es_PO_FILES})
9+
get_filename_component (es_PO_LANG ${es_PO_INPUT} NAME_WE)
10+
MESSAGE("LANG = ${es_PO_LANG}")
11+
set (es_MO_OUTPUT ${es_PO_INPUT}/LC_MESSAGES/emulationstation.mo)
12+
# update the po from the pot
13+
add_custom_command (TARGET i18n COMMAND basename ${es_PO_INPUT} && ${MSGMERGE_EXECUTABLE} -U --no-fuzzy-matching ${es_PO_INPUT}/LC_MESSAGES/emulationstation.po ${es_POT})
14+
# compile the po to mo
15+
add_custom_command (TARGET i18n COMMAND ${MSGFMT_EXECUTABLE} -o ${es_MO_OUTPUT} ${es_PO_INPUT}/LC_MESSAGES/emulationstation.po --statistics)
16+
install (FILES ${es_MO_OUTPUT} DESTINATION share/locale/${es_PO_LANG}/LC_MESSAGES RENAME emulationstation.mo)
17+
endforeach (es_PO_INPUT ${es_PO_FILES})

locale/checkLocale.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
(cd lang &&
4+
for POLANG in *
5+
do
6+
POFILE="./${POLANG}/LC_MESSAGES/emulationstation.po"
7+
echo -n "${POLANG}: "
8+
LANG=C msgfmt "${POFILE}" -o - --statistics > /dev/null
9+
done
10+
)

locale/emulationstation.pot

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3+
# This file is distributed under the same license as the PACKAGE package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5+
#
6+
#, fuzzy
7+
msgid ""
8+
msgstr ""
9+
"Project-Id-Version: PACKAGE VERSION\n"
10+
"Report-Msgid-Bugs-To: \n"
11+
"POT-Creation-Date: 2019-07-19 10:06+0200\n"
12+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14+
"Language-Team: LANGUAGE <LL@li.org>\n"
15+
"Language: \n"
16+
"MIME-Version: 1.0\n"
17+
"Content-Type: text/plain; charset=CHARSET\n"
18+
"Content-Transfer-Encoding: 8bit\n"
19+
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
20+
21+
#, c-format
22+
msgid "%i GAME AVAILABLE"
23+
msgid_plural "%i GAMES AVAILABLE"
24+
msgstr[0] ""
25+
msgstr[1] ""
26+
27+
msgid "Loading..."
28+
msgstr ""
29+
30+
msgid "Loading system config..."
31+
msgstr ""
32+
33+
msgid "QUIT"
34+
msgstr ""
35+
36+
msgid "Done."
37+
msgstr ""

0 commit comments

Comments
 (0)