Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OHRM5X-2480: Develop language package validation #1835

Draft
wants to merge 21 commits into
base: 5.7
Choose a base branch
from

Conversation

TharakaAthukorala
Copy link

No description provided.

@TharakaAthukorala TharakaAthukorala changed the base branch from 5.7-old to 5.7 April 5, 2024 04:58
@TharakaAthukorala TharakaAthukorala changed the title 5.7 old OHRM5X-2480: Develop language package validation Apr 5, 2024
Comment on lines 33 to 48
Use the sample template
</oxd-text>
</li>
<li>
<oxd-text class="orangehrm-information-card-text">
Only edit the target field.
</oxd-text>
</li>
<li>
<oxd-text class="orangehrm-information-card-text">
Do not change the template.
</oxd-text>
</li>
<li>
<oxd-text class="orangehrm-information-card-text">
Sample XLIFF :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add these as lang strings. Check these references:

create .yaml and add lang strings: https://github.com/orangehrm/orangehrm/blob/main/installer/Migration/V5_6_0/lang-string/admin.yaml

add insert code to Migration.php: https://github.com/orangehrm/orangehrm/blob/main/installer/Migration/V5_6_0/Migration.php#L44-L49

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

rules: {
attachment: [
required,
maxFileSize(1048576),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RajithaKumara we might have to implement some way to increase max file size in this release since we have had people asking before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check similar place and get as a config file

Comment on lines 165 to 168
props: {
label: 'Import',
style: 'Text',
displayType: 'text',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will change this to an icon later

public const PARAMETER_LANGUAGE_ID = 'languageId';
public const PARAMETER_ATTACHMENT = 'attachment';

public const PARAM_RULE_IMPORT_FILE_FORMAT = ['text/xml', 'application/xml', 'application/xliff+xml'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TharakaAthukorala is it possible for .xlf extension to take MIME types other than application/xliff+xml

If so we might have to apply sanitization here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The MIME type application/xliff+xml is the standard MIME type for .xlf files.
  • For .xlf files, it's theoretically possible for other MIME types to be associated with this extension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.xliff seems this also valid extension

public const PARAMETER_ATTACHMENT = 'attachment';

public const PARAM_RULE_IMPORT_FILE_FORMAT = ['text/xml', 'application/xml', 'application/xliff+xml'];
public const PARAM_RULE_IMPORT_FILE_EXTENSIONS = ['xml', 'xlf'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RajithaKumara I feel we should only allow .xlf. Tharaka has changed to export to give an .xlf file as well. Not sure if we will have additional security concerns if we allow .xml.

@@ -0,0 +1,158 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TharakaAthukorala you need to add a new data group for this API in the Migration

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Comment on lines 88 to 90
* @inheritDoc
* @throws InvalidParamException
* @throws \Exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove exceptions

Suggested change
* @inheritDoc
* @throws InvalidParamException
* @throws \Exception
* @inheritDoc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -230,23 +233,26 @@ private function getXliffXmlSources(array $i18nGroups, I18NLanguage $language):
$root->setAttribute('srcLang', 'en_US');
$root->setAttribute('trgLang', $language->getCode());
$root->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
$root->setAttribute('date', @date('Y-m-d\TH:i:s\Z'));
// $root->setAttribute('date', @date('Y-m-d\TH:i:s\Z'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

date attribute is not allowed right? we can remove this line

https://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment on lines 290 to 325
$document = new \DOMDocument();
$document->loadXML($content);

if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
$normalizedLocalePattern = sprintf(
'(%s|%s)',
preg_quote($targetLanguage, '/'),
preg_quote(str_replace('-', '_', $targetLanguage), '/')
);
// strict file names require translation files to be named '____.locale.xlf'
$expectedFilenamePattern = sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/', $normalizedLocalePattern);

if (0 === preg_match($expectedFilenamePattern, basename($file))) {
$errors[] = [
'line' => -1,
'column' => -1,
'message' => sprintf(
'There is a mismatch between the language included in the file name ("%s") and the "%s" value used in the "target-language" attribute of the file.',
basename($file),
$targetLanguage
),
];
}
}

foreach (XliffUtils::validateSchema($document) as $xmlError) {
$errors[] = [
'line' => $xmlError['line'],
'column' => $xmlError['column'],
'message' => $xmlError['message'],
];
}

libxml_clear_errors();
libxml_use_internal_errors($internal);

return ['file' => $file, 'isValid' => 0 === \count($errors), 'messages' => $errors];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noting down that code taken from XliffLintCommand in symfony/translations
https://github.com/symfony/translation/blob/5.4/Command/XliffLintCommand.php#L123-L156

We'll have to discuss this a bit. Not sure if we need the filename checking part.

Better to create a utility class for XliffUtils for now. Reference: https://github.com/orangehrm/orangehrm/blob/main/src/plugins/orangehrmCorePlugin/Utility/Sanitizer.php

$units = $xliffDocument->getElementsByTagName('unit');

// Define validation constraints
$maxLength = 255;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you decide this value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to discuss the max char length for source and target XLIFFs. If there is no limit we can remove this length validation.

Copy link
Member

@RajithaKumara RajithaKumara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API tests are pending

<div class="orangehrm-background-container">
<div class="orangehrm-card-container">
<oxd-text class="orangehrm-main-title">
Import Language Package: {{ languageName }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<ul>
<li>
<oxd-text class="orangehrm-information-card-text">
Use the sample template.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</li>
<li>
<oxd-text class="orangehrm-information-card-text">
Only edit the target field.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</li>
<li>
<oxd-text class="orangehrm-information-card-text">
Do not change the template.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle these language strings

</li>
<li>
<oxd-text class="orangehrm-information-card-text">
Sample XLIFF :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public const PARAMETER_LANGUAGE_ID = 'languageId';
public const PARAMETER_ATTACHMENT = 'attachment';

public const PARAM_RULE_IMPORT_FILE_FORMAT = ['text/xml', 'application/xml', 'application/xliff+xml'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.xliff seems this also valid extension


$file = $xml->createElement('file');
$file->setAttribute('id', 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devishke-orange check whether we have better id for this

@@ -270,4 +275,165 @@ private function getXliffXmlSources(array $i18nGroups, I18NLanguage $language):
}
return $xml;
}

public function symfonyXliffValidations(string $content, ?string $file = null): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add doc comment

$unitErrors = "";

// Validate source text
if (strlen($source) > $maxLength) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (strlen($source) > $maxLength) {
if ($this->getTextHelper()->strLength($source, '8bit') > $maxLength) {

reference: https://github.com/orangehrm/orangehrm/blob/main/src/plugins/orangehrmCorePlugin/Api/V2/Validator/Rules/Base64Attachment.php#L98

}

// Validate target text
if (strlen($target) > $maxLength) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strlen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants