Skip to content

Commit

Permalink
Merge pull request #919 from 4ian/refactor/cra2-and-jslingui
Browse files Browse the repository at this point in the history
Add support for translations in GDevelop 5 🌎

For contributors, please merge master in your branches for your Pull Requests. 
See this comment: #919 (comment)
Don't forget to remove `node_modules` folder in `newIDE/app`, and relaunch `npm install` or `yarn`, then `npm start` or `yarn start`.
  • Loading branch information
4ian authored Feb 16, 2019
2 parents 8866719 + fca6f76 commit 2fa3bb7
Show file tree
Hide file tree
Showing 255 changed files with 33,363 additions and 38,498 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/ExtLibs/*.7z
/scripts/Repository keys
/scripts/logs/*.txt
/scripts/gdcore-gdcpp-gdjs-extensions-messages.pot
/Binaries/.build*
/Binaries/.embuild*
/Binaries/build*
Expand Down
22 changes: 13 additions & 9 deletions Core/GDCore/Extensions/Builtin/TimeExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsTimeExtension(
.MarkAsAdvanced();

extension
.AddAction("ResetTimer",
_("Start (or reset) a scene timer"),
_("Reset the specified scene timer, if the timer doesn't exist "
"it's created and started."),
_("Reset the timer _PARAM1_"),
_("Timers and time"),
"res/actions/timer24.png",
"res/actions/timer.png")
.AddAction(
"ResetTimer",
_("Start (or reset) a scene timer"),
_("Reset the specified scene timer, if the timer doesn't exist "
"it's created and started."),
_("Reset the timer _PARAM1_"),
_("Timers and time"),
"res/actions/timer24.png",
"res/actions/timer.png")
.AddCodeOnlyParameter("currentScene", "")
.AddParameter("string", _("Timer's name"));

Expand Down Expand Up @@ -200,7 +201,10 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsTimeExtension(
"stringWithSelector",
_("Hour: hour - Minutes: min - Seconds: sec - Day of month: "
"mday - Months since January: mon - Year since 1900: year - Days "
"since Sunday: wday - Days since Jan 1st: yday - Timestamp (ms): timestamp\"), "[\"hour\", \"min\", \"sec\", \"mon\", \"year\", \"wday\", \"mday\", \"yday\", \"timestamp\"]");
"since Sunday: wday - Days since Jan 1st: yday - Timestamp (ms): "
"timestamp\""),
"[\"hour\", \"min\", \"sec\", \"mon\", \"year\", \"wday\", \"mday\", "
"\"yday\", \"timestamp\"]");

#endif
}
Expand Down
6 changes: 5 additions & 1 deletion Core/GDCore/Extensions/Metadata/InstructionMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

namespace gd {
InstructionMetadata::InstructionMetadata()
: sentence(_("Unknown or unsupported instruction")),
: sentence(
"Unknown or unsupported instruction"), // Avoid translating this
// string, so that it's safe
// and *fast* to use a
// InstructionMetadata.
canHaveSubInstructions(false),
hidden(true) {}

Expand Down
35 changes: 35 additions & 0 deletions Core/GDCore/Tools/Localization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* GDevelop Core
* Copyright 2008-present Florian Rival (Florian.Rival@gmail.com). All rights
* reserved. This project is released under the MIT License.
*/

#if defined(EMSCRIPTEN)
#include <emscripten.h>
#include "GDCore/String.h"

namespace gd {
gd::String GetTranslation(const char* str) { // TODO: Inline?
const char* translatedStr = (const char*)EM_ASM_INT(
{
var getTranslation = Module['getTranslation'];
if (!getTranslation) {
return $0;
}

// Uncomment lines to display a warning if the cache
// for strings is not ready.
// if (!ensureCache) {
// console.warn('No ensureCache initialized');
// return $0;
// }
ensureCache.prepare();

var translatedStr = getTranslation(Pointer_stringify($0));
return ensureString(translatedStr);
},
str);
return gd::String(translatedStr); // TODO: Is copying necessary?
}
} // namespace gd
#endif
35 changes: 35 additions & 0 deletions Core/GDCore/Tools/Localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,55 @@
#ifndef GDCORE_LOCALIZATION_H
#define GDCORE_LOCALIZATION_H

/** @file
* Provide a way to mark strings to be translated.
*
* Strings to be translated in GDevelop Core codebase (and GDCpp),
* are marked with the underscore macro, for example: _("Hello World").
*
* The macro is then defined to be using the translation function
* of the underlying platform (Emscripten for GD5, wxWidgets for GD4,
* no translation for GDCpp Runtime).
*/

#if defined(GD_IDE_ONLY) && !defined(GD_NO_WX_GUI)
// When compiling with wxWidgets, use the translation method
// provided by wxWidgets, but return a gd::String.

#include <wx/intl.h>
#include "GDCore/String.h"

// Create a new macro to return UTF8 gd::String from a translation
#if defined(_)
#undef _
#endif
#define _(s) gd::String(wxGetTranslation(wxString::FromUTF8(u8##s)))

#elif defined(EMSCRIPTEN)
// When compiling with Emscripten, use a translation function that is calling a
// JS method on the module, so that an external translation library can be used.

#include "GDCore/String.h"
#if defined(_)
#undef _
#endif

namespace gd {
gd::String GetTranslation(const char* str);
}

#define _(s) gd::GetTranslation(u8##s)

#else
// When compiling without Emscripten or wxWidgets (typically for GDC++ Runtime),
// just return an untranslated gd::String.

// Create a new macro to return UTF8 gd::String from a translation
#if defined(_)
#undef _
#endif
#define _(s) gd::String(u8##s)

#endif

#endif // GDCORE_LOCALIZATION_H
Loading

0 comments on commit 2fa3bb7

Please sign in to comment.