I have extensively used gettext system in my c++ application in linux environment for translation into different languages. Everything works fine.
Now, suppose that the whole application has been translated to English from German. But I need to query on some strings which should be in German (meaning query on the original string).
Like gettext("Energie") --> this is in German and translates to "Energy" in English and user can see that but I need the string "Energie".
There could be many solutions but I am specifically looking into gettext system. Is there way in gettext system to retrieve the original text ? Any help is appreciated.
Reverse translation like this isn't possible with gettext. Here's a simple .PO file for French:
msgid "a strawberry"
msgstr "une fraise"
msgid "a drill"
msgstr "une fraise"
You cannot reverse-translate "une fraise" back into English, as there are two possible meanings, and gettext would not know which one you meant.
Maybe you need what the Python gettext documentation calls "deferred translation": Instead of applying the translation function at definition time, rather do it at print time.
Instead of this, which will use different array indices in each language:
name = _('Energie')
dict[name] = 50
print('%s is %d' % (name, dict[name]))
do this:
name = 'Energie'
dict[name] = 50
print('%s is %d' % (_(name), dict[name]))
(Assuming that _() is used as a real translation function; this won't work if it's a preprocessor directive as I read it's often the case in C environment.)
Related
Problem
I translate my group names in my Django application with the default translation tools.
Since my group names are not hard-coded in my code, when I run makemessages, the lines corresponding to my group names are commented out.
Example
I have a group named management_product which is automatically created during migrations. I put these lines in django.po:
msgid "management_product"
msgstr "Gestion des produits"
But if I run django-admin makemessages -l fr, they are commented out:
#~ msgid "management_product"
#~ msgstr "Gestion des produits"
Question
How can I disable this behaviour?
Django translations are not meant for dynamic data translations that come from third party data sources like your db. It is meant to translate the static stuff you have in your codebase.
You may want to use something like: https://django-modeltranslation.readthedocs.io/en/latest/ instead.
# google-translate version 3.6.1
from os import environ
import os
from google.cloud import translate
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"/multilingual-bot.json"
project_id = 'multilingual-bot'
assert project_id
parent = f"projects/{project_id}"
client = translate.TranslationServiceClient()
client.get_supported_languages(parent=parent, display_language_code="en")
sample_text = "kevu chale che badhu"
target_language_code = "en"
lan_response = client.detect_language(parent=parent, content=sample_text)
response = client.translate_text(contents=[sample_text],source_language_code='gu',target_language_code=target_language_code,parent=parent)
for translation in response.translations:
print(translation.translated_text)
print(lan_response)
when i pass "kevu chale che badhu" in sample_text the output remains same but if we change it in root language i.e "કેવુ ચલે છે બધુ" it will return correct output. I change the source language code to gu as well but nothing changed.
Currently, Google Cloud Translation API does not support the translation of any language written in the Latin Alphabet to the target language.
The same has been raised as an issue in this issue tracker for hindi language. If you would like to have this feature for Gujarati language please raise a feature request using this link.However, we cannot provide an ETA at the moment but you can “STAR” the issue to receive automatic updates and give it traction by referring to this link.
The only known solution I can find, and I have successfully implmented (which handles Hindi, and other languages as well), is to transliterate the inputs.
I personally think it's a bad answer from Google that it isn't supported, because the target language that's returned from Latin-input characters (phonetically equivalent to the inputters desired speech), is the actual language. Thus "HI" is detected on Hindi, but the returned translation is just the same as the input. If Google can detect it's Hindi from the transliterated Latin-based characters, then why not just return the results?
Steps to work around:
1. translate through translate-api as normal. Use the latin characters as the inputs.
2. receive the results from the the translate, and ignore the returned translation. (It will be the same as the input latin). Collect the targetLanguageDetected field, and save this.
3. call google's (deprecated but still supported) transliterate interface, with the input language (received from step 2) and the same original latin character inputs (URL is: https://inputtools.google.com/request?text=${srcMessage}&itc=${srclang}-t-i0-und&num=${numChoices}&cp=0&cs=1&ie=utf-8&oe=utf-8&app=demopage) - srcMessage is the original latin character text, srclang is the output from step 2, and numchoices is the number of "suggestions" that are generated from the input.
4. re run step 1, with the output from step 3 (the "HI" characters, or whatever alphabet is returned)
Google used to allow detection without a translate cost, but this isn't the case anymore.
In addition, Google's API is inconsistent in this behavior. For example if I type in Russian transliteration:
"Privet", I get "Hello"
Hi google cloud console is made for english use you are not in admin fully its just what you manage example i tried shutting down my google could console it dident work i tried sudo and it worked the same way download the language package using sudo apt and install it using sudo do all work in sudo by sudo su if it does not work so that means that google does not want you to do so
When django begins translating the page, I would like to find the texts that were marked for translation, but django did not find the msgid for the same in the po/mo file
If I can find it, I would then log it for further processing
Example,
message_to_be_translated = _("Hello")
this_message_is_marked_but_wont_be_translated = _(some_variable_name_whose_value_we_dont_know_now)
I would like to log the value of "some_variable_name_whose_value_we_dont_know_now" during run time to a file or something.
The reason we wont know the value before hand is
it could be from a third party api
it could come from a user request
Edit
Where (in which method) does django look for the msgid in the translation file and say that
this string has translation id, so let me translate this one
this key doesn't have translation id (msgid) set, so let me ignore it.
I want to find the second one to be precise.
Django's default LANGUAGES settings contains zh-cn and zh-tw. However, I would like to use zh instead of zh-tw. I've set up my LANGUAGES setting appropriately and compiled the gettext messages but Django will only load the zh-cn translation.
Frustratingly, gettext will load the zh translation fine if I delete the zh_CN directory!
Is there any way to get zh to load the right translation?
My assumption on this is because django itself is only translated into zh-cn and zh-tw. Django translation will only allow you to translate into a language Django itself is translated for.
You should try to translate Django into zh (or copy zh-tw into zh).
Maybe the "Using Gettextize software" from GNU C help.
http://www.gnu.org/software/libc/manual/html_node/Using-gettextized-software.html
Quote:
The file /usr/share/locale/locale.alias (replace /usr with whatever
prefix you used for configuring the C library) contains a mapping of
alternative names to more regular names. The system manager is free to
add new entries to fill her/his own needs. The selected locale from
the environment is compared with the entries in the first column of
this file ignoring the case. If they match the value of the second
column is used instead for the further handling.
Note that the in some system (e.g. Fedora 21), /usr/system/locale/locale.alias is obsoleted and just for backward compatibility.
I have a question regarding Django i18n and the django.po file.
I am new to i18n so please excuse my noobness.
I am using the Google Tranlator Toolkit to upload a text
file of English words and phrases.
In this case I am converting to Korean.
Here is a sample of four words:
Gender
Date of Birth
Country
City / Town
Here is the output on-screen:
성별
생일
국가
도시
When I click save it saves to a text file.
My questions is is it OK to use the Korean symbols
direct into my django.po file?
Everything is working OK in my application so far.
Looking at the django/contrib/auth/local/ko/LC_MESSAGES/django.po
file I can see it's using a format like:
msgid "Password"
msgstr "비밀번호"
So far it seems to work OK saving the symbols I have
but I'm just wondering if this is the corect way to do this.
So far I'm intending to use google to translate my English
into various languages and then I'll save them each
to their respective django.po file.
Any tips or advise much appreciated.
Thanks!
Good question!
I'm doing the same for my translation into portuguese, but I'm also, not quite sure, if it's the best way to achieve this.
Be careful if you aren't doing the translation manually but using this command
django-admin.py makemessages -l de
every time you update the file, check if everything is correct, because it can do associations, for instance I've writen:
first template:
pt = 'maça e cenoura'
second template:
pt = 'maça e cereja'
doing the automatic translation, it will assume both are equal strings, which they aren't.
I'm not able to explain why, because I don't know why this happen.
A solution could be to use a string 'X' and translate to both languages the text.
msgid = 'X'
pt = 'maça e cenoura'
en = 'apple and carrot' #different files (just as example, I put them together)
But like I told you, I'm also just starting using this feature
python manage.py makemessages -l ko output django.po default is ansi format,
you must convert it to utf-8 to allow non-ansi chars in it.