How to sequence in case of multiple convertors ? Spartacus 3.4.1 - spartacus-storefront

I implemented a custom convertor that does a custom field mapping :
export class DCPOccConfiguratorVariantNormalizer extends OccConfiguratorVariantNormalizer {
...
}
and configured it via app.module.ts
{provide: VARIANT_CONFIGURATOR_NORMALIZER, useExisting: DCPOccConfiguratorVariantNormalizer, multi: true},
Now during runtime, both convertors are called (first custom, second standard). Therefore, the custom field mapping of the custom convertor is beeing overwritten again by the standard convertor.
How can the convertor execution sequence be configured? Is there any other way to change the field mapping of a normalizer/convertor?

Related

Translations Service

I'm looking for a solution/idea to dynamically change the translation value of each key in Sparatcus translations files outside the code. I don't want only to create a file and override the I18nModule config, I'm looking for some kind of service/API like Lokalize API to be able to change the translation values outside the code.
Thanks in advance!
The internationalisation features are prepared for this. Although we do not have a service at hand for the localised labels, Spartacus is prepared for this. You can read more about this at https://sap.github.io/spartacus-docs/i18n/#lazy-loading. You can configure loadPath to an API endpoint, including the variable language (lng) and namespace (ns).
imports: [
B2cStorefrontModule.withConfig({
i18n: {
backend: {
loadPath: 'assets/i18n-assets/{{lng}}/{{ns}}.json'
// crossOrigin: true (use this option when i18n assets come from a different domain)
},
chunks: translationChunksConfig
}
})
];

How to inject external JS library into a module?

I'm using SeedStack to create a web application. In order to do that, I use W20 to develop my frontend. I need specific JavaScript libraries into that project. How can I inject an external javascript library into it ? I want to use Chart.js http://www.chartjs.org/ to visualize data into charts. To do that, I suppose that I have to inject ChartJS as a dependency module in Angular.
Thank you for your help.
Before getting to the specific answer, please note that SeedStack already has an add-on for charts. As for integrating a library with W20, you have two main things to do:
Configure RequireJS to load the JS file and be able to inject it as a dependency.
Integrate the library with the AngularJS framework, which is often done by writing some directive.
Fortunately for you, Angular directives are already available for Chart.js, thanks to the angular-chart.js library. You just need to configure RequireJS to load it. Add a requireConfig section to the manifest of one of your fragments:
{
"id": "my-fragment",
...
"requireConfig": {
"paths": {
"{angular-chart.js}": "${components-path:bower_components}/angular-chart.js/dist",
"{chart.js}": "${components-path:bower_components}/chart.js/dist"
},
"map": {
"{angular-chart.js}/angular-chart": {
"angular": "{angular}/angular",
"chart": "{chart.js}/Chart"
}
}
}
}
The paths section declares locations of the two Chart.js libraries. Note that we use a variable named components-path with a default value of bower_components here. This is useful when using the W20 bridge add-on.
The map section declares a mapping between the expected and the real paths for dependencies of angular-chart.js.
You can then use the angular-chart.js library according to its documentation:
define([
'{angular}/angular',
'{angular-chart.js}/angular-chart',
], function(angular) {
var module = angular.module('myModule', ['ngResource', 'chart.js']);
module.controller('ContentController', [ '$scope', function($scope) {
// your JS code here
// (with your markup in a corresponding angular template)
}]);
});

using multiple translation files in aurelia i18N

I have a working app using aurelia-i18n. I would like to split translation.json file into multiple files like nav.json, message.json, etc but I am not sure how to do it.
This is how it looks right now.
locale
|-en
|- translation.json
But I want to change it to this way.
locale
|-en
|- nav.json
|- message.json
Is it possible to do it? If so, how do I configure it and access values in each file?
You can have multiple resource files and these are called namespaces in the i18next library (by default you only have one namespace which is called: translation) which is used by aurelia i18N.
You just need to list your namespaces when configuring the plugin with the namespaces and defaultNs properties inside the ns option:
.plugin('aurelia-i18n', (instance) => {
// adapt options to your needs (see http://i18next.com/pages/doc_init.html)
instance.setup({
resGetPath : 'locale/__lng__/__ns__.json',
lng : 'de',
attributes : ['t','i18n'],
ns: {
namespaces: ['nav', 'message'],
defaultNs: 'message'
},
getAsync : true,
sendMissing : false,
fallbackLng : 'en',
debug : false
});
});
See also the documentation of i18next and this related github issue: Using namespaces

Extending Laravel 5.1 Mess Detection the right way

Laravel 5.1 has only one built-in hardcoded Mess Detection, it searches to see if #package tag is the same with /namespace, like so:
preg_match('/namespace\ ' . self::REQUIRED_NAMESPACE . '\\\(.*);/isU', $content, $namespace);
preg_match('/\/*( )+#package\ (.*)[\r?\n]/isU', $content, $package);
if (!isset($namespace[1]) || empty($namespace[1])) {
continue;
}
I want to extend this to add multiple detection classes.
My folder structure currently looks like this:
Helpers >
Commands >
MessDetector >
Detector >
DetectorInterface.php
DetectorAbstract.php
PackageTagDetector.php
MessDetector.php
The file MessDetector is made as an Artisan Command, that means it extends \Illuminate\Console\Command
I have a config file called mess_detection.php which has the detection rules like so:
return [
'rules' => [
'\Helpers\Commands\MessDetector\PackageTagDetector' =>
[
'rule' => '/\/*( )+#package\ (.*)[\r?\n]/isU',
'path' => app_path(),
'info' => 'Checking for #package tag',
'error'=> 'Malformed or missing package tag'
]
]
];
But got stuck with figuring out how exactly to instantiate PackageTagDetector class in MessDetector class.
Any ideas design pattern wise?
Laravel has no built in mess detector.
Also checking if package tags matches the namespace is a custom convention, Laravel doesn't use package tags at all.
It seems like you are using a third party library?
I have made an abstract class and interfaces for every rule I needed.
Each rule had it's own class.
The rules extended the abstract and in the abstract most of the logic was used.
The rules themselves where stored in a config file, in the config file I also mentioned the class and class specific actions.

RquireJS with Module in TypeScript

I'm studing TypeScript and RequireJS.
I want to simple module require but module type information missing.
Is there smart solution in such situation?
requirejs(['backbone'], (Backbone) => {
// In this function.
// Backbone is 'any'
});
requirejs(['backbone'], (BackboneRef: Backbone) => {
// error : Type reference cannot refer to container
// 型参照でコンテナー 'Backbone' を参照できません。
});
To do so you need to do the following:
Download backbone.d.ts from https://github.com/borisyankov/DefinitelyTyped, the backbone.d.ts provides typescript strongly-typed interface, if you use IDE like Visual Studio, you can have all the intellisense support to Backbone
(Optional) Config backbone in RequireJS
In your TypeScript class, you can reference Backbone like the following
`
/// <amd-dependency path="backbone" />;
/// <reference path="path/to//backbone.d.ts" />;
export class YourModel extends Backbone.Model {
}
The amd-dependency tells the compiler how to reference backbone, so it would generate the correct define statement in JavaScript.
The reference provides a way to Backbone's definition for typed check.
Hope this helps! TypeScript eliminates the hell of writing long define or require js statement, which can be error-prone in situation where there are lots of dependencies.