Convert pot file to array
Clash 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?
i18n-l10n
add a comment |Â
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?
i18n-l10n
add a comment |Â
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?
i18n-l10n
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
i18n-l10n
asked 11 hours ago
Jdrupal
1,4941525
1,4941525
add a comment |Â
add a comment |Â
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),
));
add a comment |Â
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);
add a comment |Â
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),
));
add a comment |Â
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),
));
add a comment |Â
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),
));
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),
));
answered 9 hours ago
Cesar Moore
918317
918317
add a comment |Â
add a comment |Â
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);
add a comment |Â
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);
add a comment |Â
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);
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);
edited 9 hours ago
answered 9 hours ago
kiamlalunoâ¦
78.8k9125244
78.8k9125244
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password