Issue #1658842 by penyaskito, attiks, webflo: Added Introduce a translations:// stream wrapper to access the .po file directory.

8.0.x
webchick 2012-09-06 23:26:10 -07:00
parent 8e5650ac20
commit 64d0af8c50
6 changed files with 57 additions and 18 deletions

View File

@ -165,7 +165,7 @@ class PoStreamReader implements PoStreamInterface, PoReaderInterface {
*/
public function open() {
if (!empty($this->_uri)) {
$this->_fd = fopen($this->_uri, 'rb');
$this->_fd = fopen(drupal_realpath($this->_uri), 'rb');
$this->_size = ftell($this->_fd);
$this->readHeader();
}

View File

@ -39,6 +39,14 @@ class LocaleExportTest extends WebTestBase {
$this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
$this->drupalLogin($this->admin_user);
// Set the translation file directory to something writable.
$destination = 'translations://';
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
// Copy test po files to the same directory.
file_unmanaged_copy(drupal_get_path('module', 'locale') . '/tests/test.de.po', $destination, FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'locale') . '/tests/test.xx.po', $destination, FILE_EXISTS_REPLACE);
}
/**

View File

@ -35,8 +35,13 @@ class LocaleFileImportStatus extends WebTestBase {
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages'));
$this->drupalLogin($admin_user);
// Set the translation file directory.
variable_set('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests');
// Set the translation file directory to something writable.
$destination = 'translations://';
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
// Copy test po files to the same directory.
file_unmanaged_copy(drupal_get_path('module', 'locale') . '/tests/test.de.po', $destination, FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'locale') . '/tests/test.xx.po', $destination, FILE_EXISTS_REPLACE);
}
/**
@ -79,8 +84,7 @@ class LocaleFileImportStatus extends WebTestBase {
* A file object of type stdClass.
*/
function mockImportedPoFile($langcode, $timestamp_difference = 0) {
$dir = variable_get('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests');
$testfile_uri = $dir . '/test.' . $langcode . '.po';
$testfile_uri = 'translations://test.' . $langcode . '.po';
$file = locale_translate_file_create($testfile_uri);
$file->original_timestamp = $file->timestamp;
@ -189,13 +193,10 @@ class LocaleFileImportStatus extends WebTestBase {
* Delete translation files after deleting a language.
*/
function testDeleteLanguage() {
$dir = conf_path() . '/files/translations';
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
variable_set('locale_translate_file_directory', $dir);
$langcode = 'de';
$this->addLanguage($langcode);
$file_uri = $dir . '/po_' . $this->randomName() . '.' . $langcode . '.po';
file_put_contents($file_uri, $this->randomName());
$file_uri = 'translations://po_' . $this->randomName() . '.' . $langcode . '.po';
file_put_contents(drupal_realpath($file_uri), $this->randomName());
$this->assertTrue(is_file($file_uri), 'Translation file is created.');
language_delete($langcode);
$this->assertTrue($file_uri);

View File

@ -36,8 +36,13 @@ class LocaleImportFunctionalTest extends WebTestBase {
function setUp() {
parent::setUp();
// Set the translation file directory.
variable_set('locale_translate_file_directory', drupal_get_path('module', 'locale') . '/tests');
// Set the translation file directory to something writable.
$destination = 'translations://';
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
// Copy test po files to the same directory.
file_unmanaged_copy(drupal_get_path('module', 'locale') . '/tests/test.de.po', $destination, FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'locale') . '/tests/test.xx.po', $destination, FILE_EXISTS_REPLACE);
$this->admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
$this->drupalLogin($this->admin_user);

View File

@ -105,7 +105,7 @@ function locale_translate_import_form($form, &$form_state) {
*/
function locale_translate_import_form_submit($form, &$form_state) {
// Ensure we have the file uploaded.
if ($file = file_save_upload('file', $form['file']['#upload_validators'])) {
if ($file = file_save_upload('file', $form['file']['#upload_validators'], 'translations://')) {
// Add language, if not yet supported.
$language = language_load($form_state['values']['langcode']);
@ -357,7 +357,14 @@ function locale_translate_batch_import_files($options, $force = FALSE) {
*/
function locale_translate_get_interface_translation_files($langcode = NULL) {
$directory = variable_get('locale_translate_file_directory', conf_path() . '/files/translations');
return file_scan_directory($directory, '!' . (!empty($langcode) ? '\.' . preg_quote($langcode, '!') : '') . '\.po$!', array('recurse' => FALSE));
$return = file_scan_directory($directory, '!' . (!empty($langcode) ? '\.' . preg_quote($langcode, '!') : '') . '\.po$!', array('recurse' => FALSE));
foreach ($return as $filepath => $file) {
$file->uri = 'translations://' . $file->filename;
$return[$file->uri] = $file;
unset($return[$filepath]);
}
return $return;
}
/**
@ -440,8 +447,10 @@ function locale_translate_batch_import($filepath, $options, &$context) {
);
// The filename is either {langcode}.po or {prefix}.{langcode}.po, so
// we can extract the language code to use for the import from the end.
if ($options['langcode'] || preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $matches)) {
$file = entity_create('file', array('filename' => drupal_basename($filepath), 'uri' => $filepath));
if (isset($options['langcode']) && $options['langcode'] ||
preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $matches)) {
$basename = drupal_basename($filepath);
$file = entity_create('file', array('filename' => $basename, 'uri' => 'translations://'. $basename));
// We need only the last match, but only if the langcode is not explicitly
// specified in the $options array.
if (!$options['langcode'] && is_array($matches)) {
@ -450,7 +459,7 @@ function locale_translate_batch_import($filepath, $options, &$context) {
try {
if (empty($context['sandbox'])) {
$context['sandbox']['parse_state'] = array(
'filesize' => filesize($file->uri),
'filesize' => filesize(drupal_realpath($file->uri)),
'chunk_size' => 200,
'seek' => 0,
);
@ -551,7 +560,7 @@ function locale_translate_file_create($filepath) {
$file = new stdClass();
$file->filename = drupal_basename($filepath);
$file->uri = $filepath;
$file->timestamp = filemtime($file->uri);
$file->timestamp = filemtime(drupal_realpath($file->uri));
return $file;
}

View File

@ -13,6 +13,7 @@
use Drupal\locale\LocaleLookup;
use Drupal\locale\LocaleConfigSubscriber;
use Drupal\locale\TranslationsStream;
/**
* Regular expression pattern used to localize JavaScript strings.
@ -170,6 +171,21 @@ function locale_theme() {
);
}
/**
* Implements hook_stream_wrappers().
*/
function locale_stream_wrappers() {
$wrappers = array(
'translations' => array(
'name' => t('Translation files'),
'class' => 'Drupal\locale\TranslationsStream',
'description' => t('Translation files'),
'type' => STREAM_WRAPPERS_LOCAL_NORMAL,
),
);
return $wrappers;
}
/**
* Implements hook_language_insert().
*/