Convert pot file to array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












So, what I need to do is to convert the contents of a .pot file to a PHP array.



I know that there is that function called _locale_import_read_po().



I'we tried to use this function with a .pot file, like this:



$translations = _locale_import_read_po('db-store', 'translations.pot');


The translations.pot file is located in the same directory as the written php code.



My problem is that when I dpm($translations), it fails and says:




Error message



The translation import failed, because the file could not be read.




This is the code from the _locale_import_read_po() function, that makes the error.



// The file will get closed by PHP on returning from this function.
$fd = fopen($file->uri, 'rb');
if (!$fd)
_locale_import_message('The translation import failed, because the file %filename could not be read.', $file);
return FALSE;



As far as I know the 'rb' file open mode is to open non-text files.



Anyone who knows any tricks?










share|improve this question



























    up vote
    2
    down vote

    favorite












    So, what I need to do is to convert the contents of a .pot file to a PHP array.



    I know that there is that function called _locale_import_read_po().



    I'we tried to use this function with a .pot file, like this:



    $translations = _locale_import_read_po('db-store', 'translations.pot');


    The translations.pot file is located in the same directory as the written php code.



    My problem is that when I dpm($translations), it fails and says:




    Error message



    The translation import failed, because the file could not be read.




    This is the code from the _locale_import_read_po() function, that makes the error.



    // The file will get closed by PHP on returning from this function.
    $fd = fopen($file->uri, 'rb');
    if (!$fd)
    _locale_import_message('The translation import failed, because the file %filename could not be read.', $file);
    return FALSE;



    As far as I know the 'rb' file open mode is to open non-text files.



    Anyone who knows any tricks?










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      So, what I need to do is to convert the contents of a .pot file to a PHP array.



      I know that there is that function called _locale_import_read_po().



      I'we tried to use this function with a .pot file, like this:



      $translations = _locale_import_read_po('db-store', 'translations.pot');


      The translations.pot file is located in the same directory as the written php code.



      My problem is that when I dpm($translations), it fails and says:




      Error message



      The translation import failed, because the file could not be read.




      This is the code from the _locale_import_read_po() function, that makes the error.



      // The file will get closed by PHP on returning from this function.
      $fd = fopen($file->uri, 'rb');
      if (!$fd)
      _locale_import_message('The translation import failed, because the file %filename could not be read.', $file);
      return FALSE;



      As far as I know the 'rb' file open mode is to open non-text files.



      Anyone who knows any tricks?










      share|improve this question













      So, what I need to do is to convert the contents of a .pot file to a PHP array.



      I know that there is that function called _locale_import_read_po().



      I'we tried to use this function with a .pot file, like this:



      $translations = _locale_import_read_po('db-store', 'translations.pot');


      The translations.pot file is located in the same directory as the written php code.



      My problem is that when I dpm($translations), it fails and says:




      Error message



      The translation import failed, because the file could not be read.




      This is the code from the _locale_import_read_po() function, that makes the error.



      // The file will get closed by PHP on returning from this function.
      $fd = fopen($file->uri, 'rb');
      if (!$fd)
      _locale_import_message('The translation import failed, because the file %filename could not be read.', $file);
      return FALSE;



      As far as I know the 'rb' file open mode is to open non-text files.



      Anyone who knows any tricks?







      i18n-l10n






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 11 hours ago









      Jdrupal

      1,4941525




      1,4941525




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The $file parameter needs to be a Drupal file object corresponding to the PO file to import.



          You can create one using the file_save function:



          $file = file_save((object)array(
          'filename' => $file_name,
          'uri' => $destination_uri,
          'status' => FILE_STATUS_PERMANENT,
          'filemime' => file_get_mimetype($destination_uri),
          ));





          share|improve this answer



























            up vote
            2
            down vote













            The second parameter of _locale_import_read_po() is a Drupal file object, which in Drupal 7 is simply a stdClass object.



            The quicker way to use that function and pass a file object as it expects is using code similar to the following one.



             $po_files = file_scan_directory('./', '^translation.po$', array('recurse' => FALSE));
            if (count($po_files))
            require_once DRUPAL_ROOT . '/includes/locale.inc';
            $po_file = reset($po_files);
            _locale_import_read_po('db-store', $po_file);



            Notice that the second argument of file_scan_directory() is a preg_match() regular expression, not a filename.



            Using that code, you don't need to save the file using the file API, which is essentially useless, since there is already a file in the file system.
            file_save() stores the file in the database; for a file that is used for a single operation like that, it's not necessary.



            I used st() as example for writing the code I shown in this answer.



            You could also make the code I show more generic to accept a regular expression and a directory name, and use it to load more files.



            function mymodule_import_translations($directory, $pattern) 
            $po_files = file_scan_directory($directory, $pattern, array('recurse' => FALSE));
            if (count($po_files))
            require_once DRUPAL_ROOT . '/includes/locale.inc';
            foreach ($po_files as $po_file)
            _locale_import_read_po('db-store', $po_file);








            share|improve this answer






















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "220"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f270632%2fconvert-pot-file-to-array%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              The $file parameter needs to be a Drupal file object corresponding to the PO file to import.



              You can create one using the file_save function:



              $file = file_save((object)array(
              'filename' => $file_name,
              'uri' => $destination_uri,
              'status' => FILE_STATUS_PERMANENT,
              'filemime' => file_get_mimetype($destination_uri),
              ));





              share|improve this answer
























                up vote
                1
                down vote



                accepted










                The $file parameter needs to be a Drupal file object corresponding to the PO file to import.



                You can create one using the file_save function:



                $file = file_save((object)array(
                'filename' => $file_name,
                'uri' => $destination_uri,
                'status' => FILE_STATUS_PERMANENT,
                'filemime' => file_get_mimetype($destination_uri),
                ));





                share|improve this answer






















                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  The $file parameter needs to be a Drupal file object corresponding to the PO file to import.



                  You can create one using the file_save function:



                  $file = file_save((object)array(
                  'filename' => $file_name,
                  'uri' => $destination_uri,
                  'status' => FILE_STATUS_PERMANENT,
                  'filemime' => file_get_mimetype($destination_uri),
                  ));





                  share|improve this answer












                  The $file parameter needs to be a Drupal file object corresponding to the PO file to import.



                  You can create one using the file_save function:



                  $file = file_save((object)array(
                  'filename' => $file_name,
                  'uri' => $destination_uri,
                  'status' => FILE_STATUS_PERMANENT,
                  'filemime' => file_get_mimetype($destination_uri),
                  ));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 9 hours ago









                  Cesar Moore

                  918317




                  918317






















                      up vote
                      2
                      down vote













                      The second parameter of _locale_import_read_po() is a Drupal file object, which in Drupal 7 is simply a stdClass object.



                      The quicker way to use that function and pass a file object as it expects is using code similar to the following one.



                       $po_files = file_scan_directory('./', '^translation.po$', array('recurse' => FALSE));
                      if (count($po_files))
                      require_once DRUPAL_ROOT . '/includes/locale.inc';
                      $po_file = reset($po_files);
                      _locale_import_read_po('db-store', $po_file);



                      Notice that the second argument of file_scan_directory() is a preg_match() regular expression, not a filename.



                      Using that code, you don't need to save the file using the file API, which is essentially useless, since there is already a file in the file system.
                      file_save() stores the file in the database; for a file that is used for a single operation like that, it's not necessary.



                      I used st() as example for writing the code I shown in this answer.



                      You could also make the code I show more generic to accept a regular expression and a directory name, and use it to load more files.



                      function mymodule_import_translations($directory, $pattern) 
                      $po_files = file_scan_directory($directory, $pattern, array('recurse' => FALSE));
                      if (count($po_files))
                      require_once DRUPAL_ROOT . '/includes/locale.inc';
                      foreach ($po_files as $po_file)
                      _locale_import_read_po('db-store', $po_file);








                      share|improve this answer


























                        up vote
                        2
                        down vote













                        The second parameter of _locale_import_read_po() is a Drupal file object, which in Drupal 7 is simply a stdClass object.



                        The quicker way to use that function and pass a file object as it expects is using code similar to the following one.



                         $po_files = file_scan_directory('./', '^translation.po$', array('recurse' => FALSE));
                        if (count($po_files))
                        require_once DRUPAL_ROOT . '/includes/locale.inc';
                        $po_file = reset($po_files);
                        _locale_import_read_po('db-store', $po_file);



                        Notice that the second argument of file_scan_directory() is a preg_match() regular expression, not a filename.



                        Using that code, you don't need to save the file using the file API, which is essentially useless, since there is already a file in the file system.
                        file_save() stores the file in the database; for a file that is used for a single operation like that, it's not necessary.



                        I used st() as example for writing the code I shown in this answer.



                        You could also make the code I show more generic to accept a regular expression and a directory name, and use it to load more files.



                        function mymodule_import_translations($directory, $pattern) 
                        $po_files = file_scan_directory($directory, $pattern, array('recurse' => FALSE));
                        if (count($po_files))
                        require_once DRUPAL_ROOT . '/includes/locale.inc';
                        foreach ($po_files as $po_file)
                        _locale_import_read_po('db-store', $po_file);








                        share|improve this answer
























                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          The second parameter of _locale_import_read_po() is a Drupal file object, which in Drupal 7 is simply a stdClass object.



                          The quicker way to use that function and pass a file object as it expects is using code similar to the following one.



                           $po_files = file_scan_directory('./', '^translation.po$', array('recurse' => FALSE));
                          if (count($po_files))
                          require_once DRUPAL_ROOT . '/includes/locale.inc';
                          $po_file = reset($po_files);
                          _locale_import_read_po('db-store', $po_file);



                          Notice that the second argument of file_scan_directory() is a preg_match() regular expression, not a filename.



                          Using that code, you don't need to save the file using the file API, which is essentially useless, since there is already a file in the file system.
                          file_save() stores the file in the database; for a file that is used for a single operation like that, it's not necessary.



                          I used st() as example for writing the code I shown in this answer.



                          You could also make the code I show more generic to accept a regular expression and a directory name, and use it to load more files.



                          function mymodule_import_translations($directory, $pattern) 
                          $po_files = file_scan_directory($directory, $pattern, array('recurse' => FALSE));
                          if (count($po_files))
                          require_once DRUPAL_ROOT . '/includes/locale.inc';
                          foreach ($po_files as $po_file)
                          _locale_import_read_po('db-store', $po_file);








                          share|improve this answer














                          The second parameter of _locale_import_read_po() is a Drupal file object, which in Drupal 7 is simply a stdClass object.



                          The quicker way to use that function and pass a file object as it expects is using code similar to the following one.



                           $po_files = file_scan_directory('./', '^translation.po$', array('recurse' => FALSE));
                          if (count($po_files))
                          require_once DRUPAL_ROOT . '/includes/locale.inc';
                          $po_file = reset($po_files);
                          _locale_import_read_po('db-store', $po_file);



                          Notice that the second argument of file_scan_directory() is a preg_match() regular expression, not a filename.



                          Using that code, you don't need to save the file using the file API, which is essentially useless, since there is already a file in the file system.
                          file_save() stores the file in the database; for a file that is used for a single operation like that, it's not necessary.



                          I used st() as example for writing the code I shown in this answer.



                          You could also make the code I show more generic to accept a regular expression and a directory name, and use it to load more files.



                          function mymodule_import_translations($directory, $pattern) 
                          $po_files = file_scan_directory($directory, $pattern, array('recurse' => FALSE));
                          if (count($po_files))
                          require_once DRUPAL_ROOT . '/includes/locale.inc';
                          foreach ($po_files as $po_file)
                          _locale_import_read_po('db-store', $po_file);









                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 9 hours ago

























                          answered 9 hours ago









                          kiamlaluno♦

                          78.8k9125244




                          78.8k9125244



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f270632%2fconvert-pot-file-to-array%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?