Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for contextual translation and small gettext corrections #17

Merged
merged 8 commits into from
Nov 6, 2022

Conversation

zothma
Copy link
Contributor

@zothma zothma commented Nov 6, 2022

What?

Added support for contextual translation, that means translating a same English word/sentence in several different words/sentences depending on the context.

Why?

As explained in this issue comment, one same word has to be translated differently depending on where it is in the UI.

How?

This version adds the pgettext function in src/helpers/contextualtranslation.hpp, which allows to add a context as first argument. This context is then added in the po file with the msgctxt property automatically. Several entries can now have the same id but are differentiated by their context.

Small tweaks

This version also takes care of :

@nlogozzo
Copy link
Member

nlogozzo commented Nov 6, 2022

If you don't mind, I wouldn't add these new functions to a separate contextualtranslation.hpp file. Could you combine it with translation.hpp?

@zothma
Copy link
Contributor Author

zothma commented Nov 6, 2022

That's what I first did, but it then throws a lot of warnings because the function is not used in every file depending on "translation.hpp"

@nlogozzo
Copy link
Member

nlogozzo commented Nov 6, 2022

You'd have to split it into a .hpp and .cpp file for C++ to not throw multiple definition warnings.

Try implementing it like this:

src/helpers/translation.hpp

#pragma once

#include <libintl.h>
#include <locale.h>

#define _(String) gettext(String)

const char* pgettext_aux(const char* msg_ctxt_id, const char* msgid);

#define GETTEXT_CONTEXT_GLUE "\004"
#define pgettext(Ctxt, String) pgettext_aux(Ctxt GETTEXT_CONTEXT_GLUE String, String)

src/helpers/translation.cpp

#include "translation.hpp"

const char* pgettext_aux(const char* msg_ctxt_id, const char* msgid)
{
    const char* translation{ dcgettext(GETTEXT_PACKAGE, msg_ctxt_id, LC_MESSAGES) };
    if (translation == msg_ctxt_id)
    {
    	return msgid;
    }
    return translation;
}

(Don't forget to add the translation.cpp to the src/meson.build)

@nlogozzo nlogozzo added this to the V2022.11.0 milestone Nov 6, 2022
@zothma
Copy link
Contributor Author

zothma commented Nov 6, 2022

I'm pretty sure I added something like that and I always ended up having the linker not recognizing my function... And now for some obscure reason it works... I committed these changes

@nlogozzo
Copy link
Member

nlogozzo commented Nov 6, 2022

It might have been because you put the #define above the aux method which would cause it to not see the aux method since it wasn't defined before. But who knows 🤷

@nlogozzo
Copy link
Member

nlogozzo commented Nov 6, 2022

Just one minor thing...could you move the translation.cpp below the translation.hpp in the src/meson.build file. Should be .hpp first then .cpp

@zothma
Copy link
Contributor Author

zothma commented Nov 6, 2022

Done

@nlogozzo nlogozzo merged commit a4ec695 into NickvisionApps:main Nov 6, 2022
@nlogozzo
Copy link
Member

nlogozzo commented Nov 6, 2022

Beautiful! Thank you!

@zothma zothma deleted the feature/translation_correction branch November 6, 2022 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants