using Translations inside formula scripts?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamie
    Senior Member
    • Aug 2025
    • 115

    #1

    using Translations inside formula scripts?

    Hi,

    Just wondering how one would use translations inside the formula scripts? language\translate(LABEL, CATEGORY, SCOPE) doesn't give a whole lot of clarity on how to select a particular translation. Normally, it's language\translate('file.tag')
    Last edited by jamie; 09-30-2025, 12:50 PM.
  • rabii
    Active Community Member
    • Jun 2016
    • 1329

    #2
    it is simple

    in the documentation there is an example that explain it all

    PHP Code:
    language\translate('passwordWillBeSent''messages''User'

    So here we are translating the key passwordWillBeSent which exist under category messages of the User which will return this (Password will be sent to user's email address.)

    This is the key in the json file


    Rabii
    say hey

    Comment

    • jamie
      Senior Member
      • Aug 2025
      • 115

      #3
      rabii o, sweet, that does help, it's inverted from what I thought.

      Though I am looking to do my own translations, for example, to send out an invoice that automatically translates 'pay via bank account', or 'click here for terms and conditions'

      You have shown me how to access them, though, with the location of the translations, i worry that they aren't "upgrade safe", perhaps yuri could clear that up for me?

      Comment


      • rabii
        rabii commented
        Editing a comment
        what do you mean by

        i worry that they aren't "upgrade safe" ??
    • jamie
      Senior Member
      • Aug 2025
      • 115

      #4
      rabii well if i add or edit the system files with the next espo upgrade, they will be removed. it would be much better to have a list of my own translations that i can referance

      Comment


      • rabii
        rabii commented
        Editing a comment
        Simply add these list of translation in the metadata in custom folder and espocrm will merge them and will prioritise custom ones on original.
    • jamie
      Senior Member
      • Aug 2025
      • 115

      #5
      rabii too cool! but how will espo know what folder to look in? sorry i am well new to espo as i am sure you can tell, feel free to talk to me like a total newb that wants to learn

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1329

        #6
        jamie

        No worries i totally understand and that is why we are here to share and help each other

        totally fine. espocrm looks for two folder paths for metadata

        PHP Code:
        custom/Espo/Custom/Resources/i18n/en_US

        OR

        PHP Code:
        custom/Espo/Modules/YourModuleName/Resources/i18n/en_US

        This above for looking for translation files e.g same goes for metadata entityDefs - clientDefs - selectDefs etc

        Espocrm metedata is briallntly done and give you so much flexibility - it is very powerful. you can read more on docs

        Rabii
        say hey

        Comment


        • jamie
          jamie commented
          Editing a comment
          Hey man, you're just the best! Thanks for taking the time to explain it to
      • rabii
        Active Community Member
        • Jun 2016
        • 1329

        #7
        jamie

        translation syntax is almost identical in both backend and frontend. Since you want to use this in template helper i would advise to use translation in the front end (i don't think your use case requires backend translation) - so front-end will be suitable (js level or template level) - see below translation syntax in both backend and frontend.

        PHP level
        PHP Code:
        $this->language->translate($keywordYouWantToTranslate'messages'User::ENTITY_TYPE

        JS level
        PHP Code:
        this.translate('passwordWillBeSent''messages''User'

        Smarty (template html)
        PHP Code:
        <div>{{translate 'passwordWillBeSent' category='messages' scope='User'}}</div>​ 
        Rabii
        say hey

        Comment

        • jamie
          Senior Member
          • Aug 2025
          • 115

          #8
          Originally posted by rabii
          jamie

          translation syntax is almost identical in both backend and frontend. Since you want to use this in template helper i would advise to use translation in the front end (i don't think your use case requires backend translation) - so front-end will be suitable (js level or template level) - see below translation syntax in both backend and frontend.

          PHP level
          PHP Code:
          $this->language->translate($keywordYouWantToTranslate'messages'User::ENTITY_TYPE

          JS level
          PHP Code:
          this.translate('passwordWillBeSent''messages''User'

          Smarty (template html)
          PHP Code:
           {{translate 'passwordWillBeSent' category='messages' scope='User'}} 
          as ever more than i needed but so useful to have the smarty details as well

          Comment

          • jamie
            Senior Member
            • Aug 2025
            • 115

            #9
            Originally posted by rabii
            jamie

            Smarty (template html)
            PHP Code:
             {{translate 'passwordWillBeSent' category='messages' scope='User'}} 
            for some reason this doesn't work when i use it inside a pdf template but i can access the same translation via php also from inside the pdf template

            Comment


            • rabii
              rabii commented
              Editing a comment
              i am not sure what you mean ? there is no pdf template (when creating a custom pdf helper you return an html there is no template involved. again unless you share code it is difficult to understand you is the situation and any fixed suggest will be just a gamble.
          • jamie
            Senior Member
            • Aug 2025
            • 115

            #10
            i think the code would be a bit complicated to share as it's massive, but I'll share some snippets. What I am doing is generating a PDF using the built-in dompdf generator. This then goes into the database and grabs the template, and within the template, a few template helper PHP files are called

            The template, here you can see i call the helper pdfBankDetails and the string i want to translate, i can get the string from inside the helper but not the template its self


            HTML Code:
            <table class="mt-5">
            <tr>
            <td width="50%">
            {{translate 'bankCode' category='bankdetails' scope='PDF'}}
            {{pdfBankDetails accountNumber=cHidenBankAccountNumber iban=cIBAN}}
            </td>
            <td class="text-md text-right text-top">
            <b>{{assignedUser.salutationName}} {{assignedUserName}}<br>{{assignedUser.title}}</b><br>
            for and on behalf of<br> {{assignedUser.company}}
            </td>
            </tr>
            </table>
            The important part of the helper that works great

            PHP Code:
            $lables['bankCode'] = $language->translate('bankCode''bankdetails''PDF'); 
            And here is the translation file called PDF.json

            Code:
            {
            "bankdetails": {
            "bankdetails": "Bank Details",
            "bank": "bank",
            "swiftCode": "Swift Code",
            "IBANNo": "IBAN No",
            "bankCode": "Bank Code",
            Last edited by jamie; 10-14-2025, 01:10 PM.

            Comment

            • rabii
              Active Community Member
              • Jun 2016
              • 1329

              #11
              you can't use this {{translate 'bankCode' category='bankdetails' scope='PDF'}} inside the template - it won't work. in this case you need to create a custom helper for translating language something as below

              Create a file custom/Espo/Custom/Resources/metadata/app/templateHelpers.json:

              PHP Code:
              {
                  
              "translate""Espo\\Custom\\TemplateHelpers\\Translate"


              Then add the backend

              PHP Code:
              <?php
              // replace with your proper namespace
              namespace Espo\Custom\TemplateHelpers;
              use 
              Espo\Core\Htmlizer\Helper;
              use 
              Espo\Core\Htmlizer\Helper\Data;
              use 
              Espo\Core\Htmlizer\Helper\Result;
              use 
              Espo\Core\Di;
              class 
              Translate implements HelperDi\DefaultLanguageAware
              {
                  use 
              Di\DefaultLanguageSetter;

                  public function 
              render(Data $data): Result
                  
              {
                      
                      
              $label $data->getArgumentList()[0] ?? '';
                      
              $category $data->getArgumentList()[1] ?? null;
                      
              $scope $data->getArgumentList()[2] ?? null;
                      if (!
              $label || !is_string($label)) {
                          return 
              Result::createEmpty();
                      }
                    
                      
              $label $this->defaultLanguage->translate(
                          
              $label,
                          
              $category,
                          
              $scope
                      
              );
                      return 
              Result::create($label);
                  }
              }

              Then you can use this

              PHP Code:
              {{translate 'bankCode' category='bankdetails' scope='PDF'}} 
              Rabii
              say hey

              Comment

              • jamie
                Senior Member
                • Aug 2025
                • 115

                #12
                rabii well ain't that going to explain it all! This I might be able to use to my advantage, thanks!

                Comment


                • rabii
                  rabii commented
                  Editing a comment
                  cool
                  cheers
              Working...