Developer forum

Forum » Development » Problem with the translate function from API

Problem with the translate function from API


Reply
Hello everyone,

I'm trying to use Translate.Translate(..) dynamicweb api function from inside custom module code.

The idea is to use translation key from Translations XML file and put the value dynamically as a template tag.

sth like:
 LoopCompare.SetTag("value", Translate.Translate("Oui", true));

this line doesn't do the job. What do I miss?


the xml file in use:
<?xml version="1.0" encoding="utf-8"?>
<translations>
   
   <key name="Oui">
    <translation culture="fr-FR"><![CDATA[Oui]]></translation>
    <translation culture="es-ES"><![CDATA[Si]]></translation>
    <translation culture="pl-PL"><![CDATA[Tak]]></translation>
  </key>

</translations>

Thanks

Replies

 
Nicolai Høeg Pedersen
Reply

The Translate class is for translating the backend - and not for the template translation...

To do translations in Templates, take a look at this document:
http://engage.dynamicweb-cms.com/Admin/Public/DWSDownload.aspx?File=%2fFiles%2fFiler%2fDocumentation+portal%2fDynamicweb+CMS%2fDocumentation%2fWorking+with%2fen%2f(en-US)+Working+with+template+terminologies.pdf

 
Reply
So as I understand there is no "clean" way to use translations from my module c# code?
 
Nicolai Høeg Pedersen
Reply
Well - you don't have to do any translations in code - the template system will handle that for you... Read the PDF.
 
Reply

Thank you, I read the document but it does not resolve the problem in my case.

The value which I want to translate is dynamic which means 
I need to use conditions to chose an appropriate translation key.
I do not see the way i could accomplish this in html part of the template. 

ex.
c# code:
if (condition)
  LoopCompare.SetTag("value", Translate.Translate("Oui", true));
else if ..
  LoopCompare.SetTag("value", "12345");
else 
  LoopCompare.SetTag("value", Translate.Translate("Non", true));



html template:

<!--@LoopStart(LoopCompare)-->
<tr>
<td><!--@Name--></td>

 Here I cannot know which translation key I will want to use so I cannot set a static key in html. That's why i want to insert already translated value to the output page.
<td ><!--@value--></td> 

       <td><!--@otherTag--></td>
</tr>
<!--@LoopEnd(LoopCompare)-->

 
Nicolai Høeg Pedersen
Reply

Here is a piece of code that should work:

Have'nt testet it...

Public Shared Function test() As String
 Return TranslatelLabel("Yes")
End Function

Public Shared Function TranslatelLabel(ByVal label As String) As String
 'Will look for /Files/Templates/YourModuleSystemName/Translations.xml
 Dim trans As New Templatev2.Translation.Source("YourModuleSystemName")
 trans.LoadKeys()

 'Will return something like "en-US", "da-DK" etc. Controlled by Area setting
 Dim CultureName As String = System.Threading.Thread.CurrentThread.CurrentCulture.ToString()

 Dim key As Templatev2.Translation.Key = trans.Keys(label)

 'Check if the key exists and if it exists in the current culture
 If trans.Keys.ContainsKey(key.Name) AndAlso _
  trans.Keys(key.Name).Translations.ContainsKey(CultureName) AndAlso _
  trans.Keys(key.Name).Translations(CultureName).Value.Length > 0 Then
 label = trans.Keys(key.Name).Translations(CultureName).Value()
 End If
 Return label
End Function

 

 
Reply
Thank you for resolving my issue.

The c# code I used:


static Source transGlobal = new Tmp.Translation.Source();
        public static string TranslateLabel(string label)
        {
            string CultureName = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
            string ret = String.Empty;
            transGlobal.LoadKeys(); // using global translate.xml file
            Key key;
            transGlobal.Keys.TryGetValue(label, out key);

            if (key != null &&
                key.Translations.ContainsKey(CultureName) &&
                key.Translations[CultureName].Value.Length > 0)
                ret = key.Translations[CultureName].Value;
            return ret;
        }
 
Reply
I changed it a bit, to make it work in any content module...



    static Source transGlobal = new Dynamicweb.Templatev2.Translation.Source();
 
        public static string TranslateLabel(string label)
        {
            string CultureName = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
            string ret = String.Empty;
            transGlobal.LoadKeys();
            Dynamicweb.Templatev2.Translation.Key key = new Dynamicweb.Templatev2.Translation.Key();
            transGlobal.Keys.TryGetValue(label, out key);
 
            if (key != null &&
                key.Translations.ContainsKey(CultureName) &&
                key.Translations[CultureName].Value.Length > 0)
                ret = key.Translations[CultureName].Value;
            return ret;
        }



It works wonders. The translation file path is locked to /files/templates/translation.xml

Have fun :)

 

You must be logged in to post in the forum