Skip to content

Commit

Permalink
Merge pull request #5194 from myk002/myk_translatename
Browse files Browse the repository at this point in the history
Rename translation module functions and create new lua module
  • Loading branch information
myk002 authored Jan 12, 2025
2 parents a575946 + 9d011da commit 196824f
Show file tree
Hide file tree
Showing 34 changed files with 154 additions and 239 deletions.
6 changes: 4 additions & 2 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ Template for new versions:
## API
- ``Units::isUnitInBox``, ``Units::getUnitsInBox``: add versions accepting pos arguments
- ``Units::getVisibleName``: when acting on a unit without an impersonated identity, returns the unit's name structure instead of the associated histfig's name structure
- ``Translation::GenerateName``: generates in-game names, mirroring DF's internal logic
- ``Translation::generateName``: generates in-game names, mirroring DF's internal logic

## Lua
- ``dfhack.units.isUnitInBox``, ``dfhack.units.getUnitsInBox``: add versions accepting pos arguments
- ``widgets.FilteredList``: search keys for list items can now be functions that return a string
- ``dfhack.GenerateName``: Lua API for ``Translation::GenerateName``
- ``dfhack.translation.generateName``: Lua API for ``Translation::generateName``

## Removed
- ``dfhack.TranslateName`` has been renamed to ``dfhack.translation.translateName``
- ``Translation::TranslateName`` has been renamed to ``Translation::translateName``

## Internals
- Plugin command callbacks are now called with the core suspended by default so DF memory is always safe to access without extra steps
Expand Down
23 changes: 13 additions & 10 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -949,14 +949,6 @@ can be omitted.

Checks if a site (e.g., a player fort) is loaded.

* ``dfhack.TranslateName(name[,in_english[,only_last_name]])``

Convert a ``df.language_name`` (or only the last name part) to string.

* ``dfhack.GenerateName(name,language,type,major_selector,minor_selector)``

Dynamically generate a name using the same logic the game itself uses.

* ``dfhack.df2utf(string)``

Convert a string from DF's CP437 encoding to UTF-8.
Expand All @@ -969,8 +961,8 @@ can be omitted.
.. warning::

When printing CP437-encoded text to the console (for example, names returned
from ``dfhack.TranslateName()``), use ``print(dfhack.df2console(text))`` to
ensure proper display on all platforms.
from ``dfhack.units.getReadableName()``), use
``print(dfhack.df2console(text))`` to ensure proper display on all platforms.

* ``dfhack.utf2df(string)``

Expand Down Expand Up @@ -1038,6 +1030,17 @@ can be omitted.
returns an ``output, command_result`` pair. ``output`` is a single string -
see ``dfhack.internal.runCommand()`` to obtain colors as well.

Translation module
------------------

* ``dfhack.translation.translateName(name[,in_english[,only_last_name]])``

Convert a ``df.language_name`` (or only the last name part) to string.

* ``dfhack.translation.generateName(name,language,type,major_selector,minor_selector)``

Dynamically generate a name using the same logic the game itself uses.

Gui module
----------

Expand Down
1 change: 0 additions & 1 deletion library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ if(BUILD_LIBRARY)

set(MAIN_HEADERS
include/Internal.h
include/DFHack.h
include/DFHackVersion.h
include/BitArray.h
include/ColorText.h
Expand Down
12 changes: 10 additions & 2 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,8 +1374,7 @@ static const LuaWrapper::FunctionReg dfhack_module[] = {
WRAP(isWorldLoaded),
WRAP(isMapLoaded),
WRAP(isSiteLoaded),
WRAPM(Translation, TranslateName),
WRAPM(Translation, GenerateName),
WRAPN(Translation, Translation::translateName), // left for backward compatibility
WRAP(df2utf),
WRAP(utf2df),
WRAP(df2console),
Expand Down Expand Up @@ -1405,6 +1404,14 @@ static const luaL_Reg dfhack_funcs[] = {
{ NULL, NULL }
};

/***** Translation module *****/

static const LuaWrapper::FunctionReg dfhack_translation_module[] = {
WRAPM(Translation, translateName),
WRAPM(Translation, generateName),
{ NULL, NULL }
};

/***** Gui module *****/

static int gui_getDwarfmodeViewDims(lua_State *state)
Expand Down Expand Up @@ -4248,6 +4255,7 @@ void OpenDFHackApi(lua_State *state)

LuaWrapper::SetFunctionWrappers(state, dfhack_module);
luaL_setfuncs(state, dfhack_funcs, 0);
OpenModule(state, "translation", dfhack_translation_module);
OpenModule(state, "gui", dfhack_gui_module, dfhack_gui_funcs);
OpenModule(state, "job", dfhack_job_module, dfhack_job_funcs);
OpenModule(state, "textures", dfhack_textures_funcs);
Expand Down
1 change: 0 additions & 1 deletion library/LuaTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ distribution.
#include "modules/Gui.h"
#include "modules/Job.h"
#include "modules/Screen.h"
#include "modules/Translation.h"
#include "modules/Units.h"

#include "df/building.h"
Expand Down
1 change: 0 additions & 1 deletion library/PlugLoad.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Core.h"
#include "DFHack.h"
#include "Debug.h"
#include "Export.h"
#include "PluginManager.h"
Expand Down
4 changes: 2 additions & 2 deletions library/RemoteTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ void DFHack::describeName(NameInfo *info, df::language_name *name)
if (name->language >= 0)
info->set_language_id(name->language);

std::string lname = Translation::TranslateName(name, false, true);
std::string lname = Translation::translateName(name, false, true);
if (!lname.empty())
info->set_last_name(DF2UTF(lname));

lname = Translation::TranslateName(name, true, true);
lname = Translation::translateName(name, true, true);
if (!lname.empty())
info->set_english_name(DF2UTF(lname));
}
Expand Down
78 changes: 0 additions & 78 deletions library/include/DFHack.h

This file was deleted.

5 changes: 3 additions & 2 deletions library/include/modules/Translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ DFHACK_EXPORT void setNickname(df::language_name *name, std::string nick);
DFHACK_EXPORT std::string capitalize(const std::string &str, bool all_words = false);

// translate a name using the loaded dictionaries
DFHACK_EXPORT std::string TranslateName (const df::language_name * name, bool inEnglish = false,
DFHACK_EXPORT std::string translateName (const df::language_name * name, bool inEnglish = false,
bool onlyLastPart = false);

DFHACK_EXPORT void GenerateName(df::language_name *name, int language_index, df::language_name_type nametype, df::language_word_table *major_selector, df::language_word_table *minor_selector);
DFHACK_EXPORT void generateName(df::language_name *name, int language_index, df::language_name_type nametype,
df::language_word_table *major_selector, df::language_word_table *minor_selector);
}
}
#endif
4 changes: 2 additions & 2 deletions library/modules/Items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,8 @@ static string get_base_desc(df::item *item) {
if (auto name = Items::getBookTitle(item); !name.empty())
return name;
if (auto artifact = get_artifact(item); artifact && artifact->name.has_name)
return Translation::TranslateName(&artifact->name) +
", " + Translation::TranslateName(&artifact->name, true) +
return Translation::translateName(&artifact->name) +
", " + Translation::translateName(&artifact->name, true) +
" (" + get_item_type_str(item) + ")";
return Items::getDescription(item, 0, true);
}
Expand Down
2 changes: 1 addition & 1 deletion library/modules/Military.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::string Military::getSquadName(int32_t squad_id)
return "";
if (squad->alias.size() > 0)
return squad->alias;
return Translation::TranslateName(&squad->name, true);
return Translation::translateName(&squad->name, true);
}

//only works for making squads for fort mode player controlled dwarf squads
Expand Down
Loading

0 comments on commit 196824f

Please sign in to comment.