Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #638 from ezsystems/ezp-25323_contenttype_name-1.4
Browse files Browse the repository at this point in the history
EZP-25323: ContentType name and description not showed if it is not translated in eng-GB
  • Loading branch information
dpobel committed Jul 1, 2016
2 parents 6e36514 + e26c1b4 commit 82afc1b
Show file tree
Hide file tree
Showing 77 changed files with 762 additions and 116 deletions.
26 changes: 26 additions & 0 deletions ApplicationConfig/Providers/LocalesMap.php
@@ -0,0 +1,26 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\ApplicationConfig\Providers;

use EzSystems\PlatformUIBundle\ApplicationConfig\Provider;

/**
* Provides the Locales conversion map from POSIX code to eZ Locale Code.
*/
class LocalesMap implements Provider
{
/** @var string */
private $eZToPosixMap;

public function __construct($localesMap)
{
$this->eZToPosixMap = $localesMap;
}

public function getConfig()
{
return array_flip($this->eZToPosixMap);
}
}
8 changes: 8 additions & 0 deletions Resources/config/services.yml
Expand Up @@ -18,6 +18,7 @@ parameters:
ezsystems.platformui.application_config.provider.session_info.class: EzSystems\PlatformUIBundle\ApplicationConfig\Providers\SessionInfo
ezsystems.platformui.application_config.provider.value.class: EzSystems\PlatformUIBundle\ApplicationConfig\Providers\Value
ezsystems.platformui.application_config.provider.anonymous_user_id.class: EzSystems\PlatformUIBundle\ApplicationConfig\Providers\AnonymousUserId
ezsystems.platformui.application_config.provider.locales_map.class: EzSystems\PlatformUIBundle\ApplicationConfig\Providers\LocalesMap
ezsystems.platformui.application_config.aggregator.class: EzSystems\PlatformUIBundle\ApplicationConfig\Aggregator
ezsystems.platformui.application_config.provider.root_info.class: EzSystems\PlatformUIBundle\ApplicationConfig\Providers\RootInfo
ezsystems.platformui.application_config.provider.languages.class: EzSystems\PlatformUIBundle\ApplicationConfig\Providers\Languages
Expand Down Expand Up @@ -81,6 +82,13 @@ services:
tags:
- {name: ezsystems.platformui.application_config_provider, key: 'countriesInfo'}

ezsystems.platformui.application_config.provider.locales_map:
class: %ezsystems.platformui.application_config.provider.locales_map.class%
arguments:
- %ezpublish.locale.conversion_map%
tags:
- {name: ezsystems.platformui.application_config_provider, key: 'localesMap'}

ezsystems.platformui.application_config.provider.image_variations:
class: %ezsystems.platformui.application_config.provider.value.class%
arguments:
Expand Down
7 changes: 6 additions & 1 deletion Resources/config/yui.yml
Expand Up @@ -201,6 +201,7 @@ system:
- 'ez-notificationhubplugin'
- 'ez-positionplugin'
- 'ez-updatetreeplugin'
- 'ez-translateproperty'
path: %ez_platformui.public_dir%/js/apps/ez-platformuiapp.js
ez-viewservice:
requires: ['base', 'parallel']
Expand Down Expand Up @@ -413,6 +414,7 @@ system:
ez-subitemlistitemview:
requires:
- 'ez-templatebasedview'
- 'ez-translateproperty'
- 'event-tap'
- 'event-valuechange'
- 'template'
Expand Down Expand Up @@ -1128,7 +1130,7 @@ system:
type: 'template'
path: %ez_platformui.public_dir%/templates/createcontentaction.hbt
ez-contenttypeselectorview:
requires: ['ez-templatebasedview', 'contenttypeselectorview-ez-template']
requires: ['ez-templatebasedview', 'ez-translateproperty', 'contenttypeselectorview-ez-template']
path: %ez_platformui.public_dir%/js/views/ez-contenttypeselectorview.js
contenttypeselectorview-ez-template:
type: 'template'
Expand Down Expand Up @@ -1301,6 +1303,9 @@ system:
ez-processable:
requires: ['view']
path: %ez_platformui.public_dir%/js/extensions/ez-processable.js
ez-translateproperty:
requires: ['base', 'array-extras']
path: %ez_platformui.public_dir%/js/extensions/ez-translateproperty.js
ez-contentinfo-attributes:
requires: ['ez-restmodel']
path: %ez_platformui.public_dir%/js/models/extensions/ez-contentinfo-attributes.js
Expand Down
18 changes: 17 additions & 1 deletion Resources/public/js/apps/ez-platformuiapp.js
Expand Up @@ -24,7 +24,7 @@ YUI.add('ez-platformuiapp', function (Y) {
* @constructor
* @extends App
*/
Y.eZ.PlatformUIApp = Y.Base.create('platformuiApp', Y.App, [], {
Y.eZ.PlatformUIApp = Y.Base.create('platformuiApp', Y.App, [Y.eZ.TranslateProperty], {
/**
* The list of available sides views. Each side view is an entry in this
* hash which contains the following properties:
Expand Down Expand Up @@ -210,6 +210,8 @@ YUI.add('ez-platformuiapp', function (Y) {
this._set('anonymousUserId', config.anonymousUserId);
delete config.anonymousUserId;
}
config.localesMap = config.localesMap || {};
this._set('localesMap', config.localesMap);

this._set('capi', new Y.eZ.CAPI(
this.get('apiRoot').replace(/\/{1,}$/, ''),
Expand Down Expand Up @@ -1050,6 +1052,20 @@ YUI.add('ez-platformuiapp', function (Y) {
readOnly: true,
value: "eng-GB"
},

/**
* Holds the Locales conversion map between eZ Locale codes and
* POSIX ones. See locale.yml
*
* @attribute localesMap
* @type {Object}
* @readOnly
* @default {}
*/
localesMap: {
readOnly: true,
value{},
},
}
});
});
Expand Up @@ -7,7 +7,7 @@ YUI.add('ez-registerlanguagehelpersplugin', function (Y) {
/**
* Provides the register language helpers plugin
*
* @module ez-registerhelpersplugin
* @module ez-registerlanguagehelpersplugin
*/
Y.namespace('eZ.Plugin');

Expand All @@ -26,6 +26,7 @@ YUI.add('ez-registerlanguagehelpersplugin', function (Y) {
Y.eZ.Plugin.RegisterLanguageHelpers = Y.Base.create('registerLanguageHelpersPlugin', Y.Plugin.Base, [], {
initializer: function () {
this._registerLanguageName();
this._registerTranslatedProperty();
},

/**
Expand All @@ -42,6 +43,21 @@ YUI.add('ez-registerlanguagehelpersplugin', function (Y) {

Y.Handlebars.registerHelper('language_name', Y.bind(app.getLanguageName, app));
},

/**
* Registers the `translate_property` handlebars helper. The
* `translate_property` helper expects a hash indexed by eZ Locale
* ('eng-GB', 'fre-FR', ...) and will try to pick the best one according
* to the browser languages configuration.
*
* @method _registerTranslatedProperty
* @protected
*/
_registerTranslatedProperty: function () {
var app = this.get('host');

Y.Handlebars.registerHelper('translate_property', Y.bind(app.translateProperty, app, app.get('localesMap')));
},
}, {
NS: 'registerLanguageHelpers',
});
Expand Down
23 changes: 20 additions & 3 deletions Resources/public/js/extensions/ez-loadmorepagination.js
Expand Up @@ -266,6 +266,24 @@ YUI.add('ez-loadmorepagination', function (Y) {
this.get('container').one('.ez-loadmorepagination-more-count').setContent(moreCount);
},

/**
* Returns the item view configuration for the given `struct`.
* It mixes the view configuration, the base item view configuration
* and the struct.
*
* @private
* @method _getItemViewConfig
* @param {Object} struct
* @return {Object}
*/
_getItemViewConfig: function (struct) {
return Y.merge(
{config: this.get('config')},
this._itemViewBaseConfig,
struct
);
},

/**
* Appends a rendered item view for the last loaded items.
*
Expand All @@ -276,11 +294,10 @@ YUI.add('ez-loadmorepagination', function (Y) {
*/
_appendItems: function (newItems) {
var content = this.get('container').one('.ez-loadmorepagination-content'),
ItemView = this._ItemView,
baseConfig = this._itemViewBaseConfig;
ItemView = this._ItemView;

newItems.forEach(function (struct) {
var itemView = new ItemView(Y.merge(baseConfig, struct));
var itemView = new ItemView(this._getItemViewConfig(struct));

itemView.addTarget(this);
this._itemViews.push(itemView);
Expand Down
155 changes: 155 additions & 0 deletions Resources/public/js/extensions/ez-translateproperty.js
@@ -0,0 +1,155 @@
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/
YUI.add('ez-translateproperty', function (Y) {
"use strict";
/**
* Provides the translate property plugin
*
* @module ez-translateproperty
*/
Y.namespace('eZ');

Y.eZ.TranslateProperty = Y.Base.create('translateProperty', Y.Base, [], {
/**
* Find the correct property translation according to browser's
* languages configuration. It basically iterates over the browser's
* languages to find a matching translation, if none matches, it takes
* the first one.
*
* @method translateProperty
* @param {Object} fullLocalesMap the Locales map between POSIX Locales
* (e.g.) fr_FR and Locales used in eZ Platform (e.g. fre-FR)
* @param {Object} property the property to translate as a hash indexed
* by eZ Locale for example:
*
* {'eng-GB': 'potatoes', 'fre-FR': 'pomme de terre'}
*
* @return {String}
*/
translateProperty: function (fullLocalesMap, property) {
var navigatorLocales = this._getNavigatorPosixLocales(),
localesMap = this._getLocalesMap(fullLocalesMap, property),
matchingPosixLocale;

Y.Array.some(navigatorLocales, function (posixLocale) {
var prefixLanguage;

if ( localesMap[posixLocale] ) {
matchingPosixLocale = posixLocale;
return true;
}
if ( posixLocale.indexOf('_') !== -1 ) {
prefixLanguage = posixLocale.split('_')[0] + '_';
} else {
prefixLanguage = posixLocale + '_';
}
matchingPosixLocale = this._findPosixLocaleByPrefix(
prefixLanguage, localesMap
);
return !!matchingPosixLocale;
}, this);

if ( matchingPosixLocale ) {
return property[localesMap[matchingPosixLocale]];
}
return property[Object.keys(property)[0]];
},

/**
* Returns the POSIX Locale matching the prefix
*
* @private
* @method _findPosixLocaleByPrefix
* @param {String} prefix e.g. 'fr'
* @param {Object} localesMap the locales map for the property
* @param {String} e.g. 'fr_fr'
*/
_findPosixLocaleByPrefix: function (prefix, localesMap) {
return Y.Array.find(Object.keys(localesMap), function (posixLocale) {
return posixLocale.indexOf(prefix) === 0;
});
},

/**
* Returns an array containing the normalized POSIX locale configured in
* the browser.
*
* @private
* @method _getNavigatorPosixLocales
* @return {Array} e.g. ['fr_fr', 'en_en', 'en']
*/
_getNavigatorPosixLocales: function () {
var navigator = this.get('navigator'),
languages = navigator.languages || [navigator.language];

return languages.map(function (language) {
return this._normalizeNavigatorLanguage(language);
}, this);
},

/**
* Returns an array containing the existing Locale in which the property
* exists.
*
* @private
* @method _getExistingPropertyLanguages
* @param {Object} property
* @param {Array} e.g. ['fre-FR', 'eng-GB']
*/
_getExistingPropertyLanguages: function (property) {
return Object.keys(property);
},

/**
* Creates a locales map for the languages in which the property exists.
*
* @private
* @method _getLocalesMap
* @param {Object} fullLocalesMap
* @param {Object} property
* @return {Object} an Object containing the locale indexed by
* normalized POSIX locales
*/
_getLocalesMap: function (fullLocalesMap, property) {
var localesMap = {},
existingLocales = this._getExistingPropertyLanguages(property);

Y.Object.each(fullLocalesMap, function (locale, posixLocale) {
if ( existingLocales.indexOf(locale) !== -1 ) {
localesMap[this._normalizeNavigatorLanguage(posixLocale)] = locale;
}
}, this);
return localesMap;
},

/**
* Normalizes browser's language
*
* @private
* @method _normalizeNavigatorLanguage
* @param {String} lang the language e.g. 'fr-FR'
* @return {String}
*/
_normalizeNavigatorLanguage: function (lang) {
return lang.toLowerCase().replace('-', '_');
},

}, {
ATTRS: {
/**
* Holds a reference to the navigator object.
*
* @attribute navigator
* @type {Navigator}
* @readOnly
*/
navigator: {
value: Y.config.win.navigator,
cloneDefaultValue: false,
readOnly: true,
},
}
});
});
Expand Up @@ -126,7 +126,9 @@ YUI.add('ez-createcontentactionview', function (Y) {
*/
contentTypeSelectorView: {
valueFn: function () {
return new Y.eZ.ContentTypeSelectorView();
return new Y.eZ.ContentTypeSelectorView({
config: this.get('config'),
});
}
},

Expand Down
3 changes: 2 additions & 1 deletion Resources/public/js/views/ez-actionbarview.js
Expand Up @@ -61,7 +61,8 @@ YUI.add('ez-actionbarview', function (Y) {
actionId: 'createContent',
label: 'Create',
priority: 210,
contentType: this.get('contentType')
contentType: this.get('contentType'),
config: this.get('config'),
}),
new Y.eZ.TranslateActionView({
actionId: "translate",
Expand Down

0 comments on commit 82afc1b

Please sign in to comment.