Skip to content

Commit

Permalink
feat: Localization feature was added nhn#45
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-marchenko authored and HerlinMatos committed Jul 2, 2020
1 parent 643e2c1 commit 192dc3b
Show file tree
Hide file tree
Showing 25 changed files with 206 additions and 100 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,18 @@ Add dependencies & initialize ImageEditor class with given element to make an im
```javascript
var ImageEditor = require('tui-image-editor');
var blackTheme = require('./js/theme/black-theme.js');
var locale_ru = { // override default English locale to your custom
'Crop': '袨斜蟹褉械蟹邪褌褜',
'Delete-all': '校写邪谢懈褌褜 胁褋褢'
// etc...
};
var instance = new ImageEditor(document.querySelector('#tui-image-editor'), {
includeUI: {
loadImage: {
path: 'img/sampleImage.jpg',
name: 'SampleImage'
},
locale: locale_ru,
theme: blackTheme, // or whiteTheme
initMenu: 'filter',
menuBarPosition: 'bottom'
Expand Down
9 changes: 7 additions & 2 deletions src/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Mask from './ui/mask';
import Icon from './ui/icon';
import Draw from './ui/draw';
import Filter from './ui/filter';
import Locale from './ui/locale/locale';

const SUB_UI_COMPONENT = {
Shape,
Expand Down Expand Up @@ -45,11 +46,11 @@ const BI_EXPRESSION_MINSIZE_WHEN_TOP_POSITION = '1300';
class Ui {
constructor(element, options, actions) {
this.options = this._initializeOption(options);

this._actions = actions;
this.submenu = false;
this.imageSize = {};
this.uiSize = {};
this._locale = new Locale(this.options.locale);
this.theme = new Theme(this.options.theme);

this._submenuChangeTransection = false;
Expand Down Expand Up @@ -231,6 +232,7 @@ class Ui {
path: '',
name: ''
},
locale: {},
menuIconPath: '',
menu: ['crop', 'flip', 'rotate', 'draw', 'shape', 'icon', 'text', 'mask', 'filter'],
initMenu: false,
Expand Down Expand Up @@ -271,6 +273,7 @@ class Ui {

// submenu ui instance
this[menuName] = new SubComponentClass(this._subMenuElement, {
locale: this._locale,
iconStyle: this.theme.getStyle('submenu.icon'),
menuBarPosition: this.options.menuBarPosition
});
Expand Down Expand Up @@ -298,12 +301,14 @@ class Ui {

selectedElement.classList.add('tui-image-editor-container');
selectedElement.innerHTML = controls({
locale: this._locale,
biImage: this.theme.getStyle('common.bi'),
iconStyle: this.theme.getStyle('menu.icon'),
loadButtonStyle: this.theme.getStyle('loadButton'),
downloadButtonStyle: this.theme.getStyle('downloadButton')
}) +
mainContainer({
locale: this._locale,
biImage: this.theme.getStyle('common.bi'),
commonStyle: this.theme.getStyle('common'),
headerStyle: this.theme.getStyle('header'),
Expand Down Expand Up @@ -340,7 +345,7 @@ class Ui {

btnElement.id = `tie-btn-${menuName}`;
btnElement.className = 'tui-image-editor-item normal';
btnElement.title = menuName.replace(/^[a-z]/g, $0 => $0.toUpperCase());
btnElement.title = this._locale.localize(menuName.replace(/^[a-z]/g, $0 => $0.toUpperCase()));
btnElement.innerHTML = menuItemHtml;

this._menuElement.appendChild(btnElement);
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import templateHtml from './template/submenu/crop';
* @ignore
*/
class Crop extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'crop',
iconStyle,
menuBarPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const DRAW_OPACITY = 0.7;
* @ignore
*/
class Draw extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'draw',
iconStyle,
menuBarPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const FILTER_OPTIONS = [
* @ignore
*/
class Filter extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'filter',
iconStyle,
menuBarPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/flip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import templateHtml from './template/submenu/flip';
* @ignore
*/
class Flip extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'flip',
iconStyle,
menuBarPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {defaultIconPath} from '../consts';
* @ignore
*/
class Icon extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'icon',
iconStyle,
menuBarPosition,
Expand Down
19 changes: 19 additions & 0 deletions src/js/ui/locale/locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Translate messages
*/
class Locale {
constructor(locale) {
this._locale = locale;
}

/**
* localize message
* @param {string} message - message who will be localized
* @returns {string}
*/
localize(message) {
return (this._locale[message]) ? this._locale[message] : message;
}
}

export default Locale;
3 changes: 2 additions & 1 deletion src/js/ui/mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import templateHtml from './template/submenu/mask';
* @ignore
*/
class Mask extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'mask',
iconStyle,
menuBarPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/rotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const COUNTERCLOCKWISE = -30;
* @ignore
*/
class Rotate extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'rotate',
iconStyle,
menuBarPosition,
Expand Down
3 changes: 2 additions & 1 deletion src/js/ui/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const SHAPE_DEFAULT_OPTION = {
* @ignore
*/
class Shape extends Submenu {
constructor(subMenuElement, {iconStyle, menuBarPosition}) {
constructor(subMenuElement, {locale, iconStyle, menuBarPosition}) {
super(subMenuElement, {
locale,
name: 'shape',
iconStyle,
menuBarPosition,
Expand Down
23 changes: 19 additions & 4 deletions src/js/ui/submenuBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
* @ignore
*/
class Submenu {
constructor(subMenuElement, {name, iconStyle, menuBarPosition, templateHtml}) {
/**
* @param {HTMLElement} subMenuElement - submenu dom element
* @param {Locale} locale - translate text
* @param {string} name - name of sub menu
* @param {Object} iconStyle - style of icon
* @param {string} menuBarPosition - position of menu
* @param {*} templateHtml - template for SubMenuElement
*/
constructor(subMenuElement, {locale, name, iconStyle, menuBarPosition, templateHtml}) {
this.selector = str => subMenuElement.querySelector(str);
this.menuBarPosition = menuBarPosition;
this.toggleDirection = menuBarPosition === 'top' ? 'down' : 'up';
this.colorPickerControls = [];
this._makeSubMenuElement(subMenuElement, {
locale,
name,
iconStyle,
templateHtml
Expand Down Expand Up @@ -59,14 +68,20 @@ class Submenu {

/**
* Make submenu dom element
* @param {HTMLElement} subMenuElement - subment dom element
* @param {HTMLElement} subMenuElement - submenu dom element
* @param {Locale} locale - translate text
* @param {string} name - submenu name
* @param {Object} iconStyle - icon style
* @param {*} templateHtml - template for SubMenuElement
* @private
*/
_makeSubMenuElement(subMenuElement, {name, iconStyle, templateHtml}) {
_makeSubMenuElement(subMenuElement, {locale, name, iconStyle, templateHtml}) {
const iconSubMenu = document.createElement('div');
iconSubMenu.className = `tui-image-editor-menu-${name}`;
iconSubMenu.innerHTML = templateHtml({iconStyle});
iconSubMenu.innerHTML = templateHtml({
locale,
iconStyle
});

subMenuElement.appendChild(iconSubMenu);
}
Expand Down
16 changes: 8 additions & 8 deletions src/js/ui/template/controls.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export default ({biImage, iconStyle: {normal, hover, disabled}, loadButtonStyle, downloadButtonStyle}) => (`
export default ({locale, biImage, iconStyle: {normal, hover, disabled}, loadButtonStyle, downloadButtonStyle}) => (`
<div class="tui-image-editor-controls">
<div class="tui-image-editor-controls-logo">
<img src="${biImage}" />
</div>
<ul class="tui-image-editor-menu">
<li id="tie-btn-undo" class="tui-image-editor-item" title="Undo">
<li id="tie-btn-undo" class="tui-image-editor-item" title="${locale.localize('Undo')}">
<svg class="svg_ic-menu">
<use xlink:href="${normal.path}#${normal.name}-ic-undo" class="enabled"/>
<use xlink:href="${disabled.path}#${disabled.name}-ic-undo" class="normal"/>
<use xlink:href="${hover.path}#${hover.name}-ic-undo" class="hover"/>
</svg>
</li>
<li id="tie-btn-redo" class="tui-image-editor-item" title="Redo">
<li id="tie-btn-redo" class="tui-image-editor-item" title="${locale.localize('Redo')}">
<svg class="svg_ic-menu">
<use xlink:href="${normal.path}#${normal.name}-ic-redo" class="enabled"/>
<use xlink:href="${disabled.path}#${disabled.name}-ic-redo" class="normal"/>
<use xlink:href="${hover.path}#${hover.name}-ic-redo" class="hover"/>
</svg>
</li>
<li id="tie-btn-reset" class="tui-image-editor-item" title="Reset">
<li id="tie-btn-reset" class="tui-image-editor-item" title="${locale.localize('Reset')}">
<svg class="svg_ic-menu">
<use xlink:href="${normal.path}#${normal.name}-ic-reset" class="enabled"/>
<use xlink:href="${disabled.path}#${disabled.name}-ic-reset" class="normal"/>
Expand All @@ -28,14 +28,14 @@ export default ({biImage, iconStyle: {normal, hover, disabled}, loadButtonStyle,
<li class="tui-image-editor-item">
<div class="tui-image-editor-icpartition"></div>
</li>
<li id="tie-btn-delete" class="tui-image-editor-item" title="Delete">
<li id="tie-btn-delete" class="tui-image-editor-item" title="${locale.localize('Delete')}">
<svg class="svg_ic-menu">
<use xlink:href="${normal.path}#${normal.name}-ic-delete" class="enabled"/>
<use xlink:href="${disabled.path}#${disabled.name}-ic-delete" class="normal"/>
<use xlink:href="${hover.path}#${hover.name}-ic-delete" class="hover"/>
</svg>
</li>
<li id="tie-btn-delete-all" class="tui-image-editor-item" title="Delete-all">
<li id="tie-btn-delete-all" class="tui-image-editor-item" title="${locale.localize('Delete-all')}">
<svg class="svg_ic-menu">
<use xlink:href="${normal.path}#${normal.name}-ic-delete-all" class="enabled"/>
<use xlink:href="${disabled.path}#${disabled.name}-ic-delete-all" class="normal"/>
Expand All @@ -49,11 +49,11 @@ export default ({biImage, iconStyle: {normal, hover, disabled}, loadButtonStyle,
<div class="tui-image-editor-controls-buttons">
<button style="${loadButtonStyle}">
Load
${locale.localize('Load')}
<input type="file" class="tui-image-editor-load-btn" />
</button>
<button class="tui-image-editor-download-btn" style="${downloadButtonStyle}">
Download
${locale.localize('Download')}
</button>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/js/ui/template/mainContainer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export default ({biImage, commonStyle, headerStyle, loadButtonStyle, downloadButtonStyle, submenuStyle}) => (`
export default ({locale, biImage, commonStyle, headerStyle, loadButtonStyle, downloadButtonStyle, submenuStyle}) => (`
<div class="tui-image-editor-main-container" style="${commonStyle}">
<div class="tui-image-editor-header" style="${headerStyle}">
<div class="tui-image-editor-header-logo">
<img style='display:none' src="${biImage}" />
</div>
<div class="tui-image-editor-header-buttons">
<button style="${loadButtonStyle}">
Load
${locale.localize('Load')}
<input type="file" class="tui-image-editor-load-btn" />
</button>
<button class="tui-image-editor-download-btn" style="${downloadButtonStyle}">
Download
${locale.localize('Download')}
</button>
</div>
</div>
Expand Down
Loading

0 comments on commit 192dc3b

Please sign in to comment.