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

feat(i18n): new locale interface, and translation configs #1710

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
fbbfde3
feat(localeInterface):addition of locale interface in options and and…
RiyaJethwa Dec 27, 2023
b54bc7a
formatting rectified
RiyaJethwa Dec 28, 2023
4894219
Merge branch 'master' into enhancement-localeInterface
RiyaJethwa Jan 12, 2024
1701c4e
Merge branch 'master' into enhancement-localeInterface
RiyaJethwa Jan 15, 2024
568da6a
feat(localeOptions):updated translations prop and addition of numberF…
RiyaJethwa Jan 22, 2024
b8813a6
Merge branch 'carbon-design-system:master' into enhancement-localeInt…
RiyaJethwa Jan 22, 2024
d7ef3df
fix(type):deleted unrequired imports
RiyaJethwa Jan 22, 2024
0ca33ac
fix(type):updated branch
RiyaJethwa Jan 22, 2024
40b9032
feat(localeOptions):updated locale object and locale conversion for axis
RiyaJethwa Jan 29, 2024
4fae08f
Merge branch 'carbon-design-system:master' into enhancement-localeInt…
RiyaJethwa Jan 29, 2024
57cf915
Merge branch 'carbon-design-system:master' into enhancement-localeInt…
RiyaJethwa Feb 3, 2024
04bc907
fix(localeInterface):axis ticks format
RiyaJethwa Feb 5, 2024
f6259bb
Merge branch 'carbon-design-system:master' into enhancement-localeInt…
RiyaJethwa Feb 5, 2024
2db59ad
fix(type):updated branch
RiyaJethwa Feb 5, 2024
e799a2a
fix(localeInterface):change in optionsObject property structure
RiyaJethwa Feb 7, 2024
2d8273d
fix(localeInterface):added locale demo
RiyaJethwa Feb 8, 2024
72459c4
fix(localeInterface):date translation for modal and colorScale
RiyaJethwa Feb 12, 2024
ab1a29c
fix(model):removing unused imports
RiyaJethwa Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/core/src/components/axes/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export class Axis extends Component {
// create the right ticks formatter
let formatter: any
const userProvidedFormatter = getProperty(axisOptions, 'ticks', 'formatter')
const { code, number: numberFormatter } = getProperty(options, 'locale')
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
const { code, number: numberFormatter } = getProperty(options, 'locale')
const { code: localeCode, number: numberFormatter } = getProperty(options, 'locale')

Copy link
Member

Choose a reason for hiding this comment

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

pls use more readable names. it's clear on this line that you're pulling a locale code, but in another line in this file when you will pass code into a function, it's not clear (from the name) what code is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok I'll keep this in mind, I have updated to localeCode.

if (isTimeScaleType) {
const timeInterval = computeTimeIntervalName(
axis.tickValues(),
Expand All @@ -232,23 +233,24 @@ export class Axis extends Component {

if (userProvidedFormatter === null) {
formatter = (t: number, i: number) =>
formatTick(t, i, axis.tickValues(), timeInterval, timeScaleOptions)
formatTick(t, i, axis.tickValues(), timeInterval, timeScaleOptions, options)
} else {
formatter = (t: number, i: number) => {
const defaultFormattedValue = formatTick(
t,
i,
axis.tickValues(),
timeInterval,
timeScaleOptions
timeScaleOptions,
options
)
return userProvidedFormatter(t, i, defaultFormattedValue)
}
}
} else {
if (userProvidedFormatter === null) {
if (scaleType === ScaleTypes.LINEAR) {
formatter = (t: any) => t.toLocaleString()
formatter = (t: any) => numberFormatter(t, code)
}
} else {
formatter = userProvidedFormatter
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/components/axes/ruler-binned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export class BinnedRuler extends Ruler {
...(getProperty(options, 'tooltip', 'showTotal') === true
? [
{
label: get(options, 'tooltip.totalLabel') || 'Total',
label:
get(options, 'locale.translations.total') ||
get(options, 'tooltip.totalLabel') ||
'Total',
value: activeDataGroupNames.reduce(
(accum: number, currentValue: any) =>
accum + parseFloat(get(sampleMatchData, `data.${currentValue}`)),
Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/components/axes/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Toolbar extends Component {
.classed('cds--overflow-menu-options__option--disabled', (d: any) => d.shouldBeDisabled())
.attr('aria-disabled', (d: any) => d.shouldBeDisabled())
.selectAll('button')
.text((d: any) => d.text)
.text((d: any) => d.title)
}

isOverflowMenuOpen() {
Expand Down Expand Up @@ -367,7 +367,6 @@ export class Toolbar extends Component {
getControlConfigs() {
const numberOfIcons = getProperty(this.getOptions(), 'toolbar', 'numberOfIcons') - 1
const controls = getProperty(this.getOptions(), 'toolbar', 'controls')

const overflowSpecificControls: any[] = []
const buttonList: any[] = []
const overflowList: any[] = []
Expand Down Expand Up @@ -468,6 +467,12 @@ export class Toolbar extends Component {
!this.services.zoom.isEmptyState()

const displayData = this.model.getDisplayData()
const options = this.model.getOptions()
const { toolbarExportAsCSV, toolbarExportAsJPG, toolbarExportAsPNG } = getProperty(
options,
'locale',
'translations'
)

let controlConfig: any
switch (controlType) {
Expand Down Expand Up @@ -539,7 +544,7 @@ export class Toolbar extends Component {
case ToolbarControlTypes.EXPORT_CSV:
controlConfig = {
id: 'toolbar-export-CSV',
title: 'Export as CSV',
title: toolbarExportAsCSV,
shouldBeDisabled: () => false,
iconSVG: {
content: this.getControlIconByType(controlType)
Expand All @@ -550,7 +555,7 @@ export class Toolbar extends Component {
case ToolbarControlTypes.EXPORT_PNG:
controlConfig = {
id: 'toolbar-export-PNG',
title: 'Export as PNG',
title: toolbarExportAsPNG,
shouldBeDisabled: () => false,
iconSVG: {
content: this.getControlIconByType(controlType)
Expand All @@ -561,7 +566,7 @@ export class Toolbar extends Component {
case ToolbarControlTypes.EXPORT_JPG:
controlConfig = {
id: 'toolbar-export-JPG',
title: 'Export as JPG',
title: toolbarExportAsJPG,
shouldBeDisabled: () => false,
iconSVG: {
content: this.getControlIconByType(controlType)
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/components/essentials/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@ export class Modal extends Component {

const options = this.model.getOptions()

const { toolbarTabularModalTitle, tabularDownloadAsCSV } = getProperty(
options,
'locale',
'translations'
)

const chartprefix = getProperty(options, 'style', 'prefix')

const tableArray = this.model.getTabularDataArray()

return `
<div class="cds--modal-container cds--modal-container">
<div class="cds--modal-header cds--modal-header">
<p class="cds--modal-header__label cds--type-delta cds--modal-header__label cds--type-delta" id="${id}__modal-title">Tabular representation</p>

<p class="cds--modal-header__label cds--type-delta cds--modal-header__label cds--type-delta" id="modal-title">${toolbarTabularModalTitle}</p>

<p class="cds--modal-header__heading cds--type-beta cds--modal-header__heading cds--type-beta" id="${id}__modal-description">${sanitizeText(
options.title
Expand Down Expand Up @@ -122,7 +129,7 @@ export class Modal extends Component {

<div class="cds--modal-footer cds--modal-footer">
<div class="${carbonPrefix}--${chartprefix}-modal-footer-spacer"></div>
<button class="cds--btn cds--btn--primary cds--btn cds--btn--primary" type="button" data-modal-primary-focus>Download as CSV</button>
<button class="cds--btn cds--btn--primary cds--btn cds--btn--primary" type="button" data-modal-primary-focus>${tabularDownloadAsCSV}</button>
</div>
</div>`
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/components/essentials/threshold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class Threshold extends Component {
const { value, axisPosition } = datum
const options = this.getOptions()
const scaleType = this.services.cartesianScales.getScaleTypeByPosition(axisPosition)

const { code, number: numberFormatter } = getProperty(options, 'locale')
// If scale is time, format the threshold date as the ticks format
if (scaleType === ScaleTypes.TIME) {
const isVertical = [AxisPositions.LEFT, AxisPositions.RIGHT].includes(axisPosition)
Expand All @@ -215,10 +215,10 @@ export class Threshold extends Component {
getProperty(timeScaleOptions, 'timeInterval')
)

return formatTick(value, 0, scale.ticks(), timeInterval, timeScaleOptions)
return formatTick(value, 0, scale.ticks(), timeInterval, timeScaleOptions, options)
}

return value.toLocaleString('en')
return numberFormatter(value, code)
}

appendThresholdLabel() {
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/components/essentials/title-meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ export class MeterTitle extends Title {
const options = this.getOptions()
const svg = this.getComponentContainer()
const { groupMapsTo } = options.data

const meterTitle = getProperty(options, 'locale', 'translations', 'meterTitle')
Copy link
Member

Choose a reason for hiding this comment

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

let's try to organize these better (e.g. options.locale.translations.meter.title

const proportional = getProperty(options, 'meter', 'proportional')

if (proportional) {
this.displayTotal()
this.displayBreakdownTitle()
} else {
// the title for a meter, is the label for that dataset
const title = svg.selectAll('text.meter-title').data([dataset[groupMapsTo]])

const title = svg
.selectAll('text.meter-title')
.data(meterTitle ? [meterTitle] : [dataset[groupMapsTo]])
title
.enter()
.append('text')
Expand Down Expand Up @@ -214,7 +215,7 @@ export class MeterTitle extends Title {
*/
appendPercentage() {
const dataValue = getProperty(this.model.getDisplayData(), 0, 'value')

const { code, number: numberFormatter } = getProperty(this.getOptions(), 'locale')
// use the title's position to append the percentage to the end
const svg = this.getComponentContainer()
const title = DOMUtils.appendOrSelect(svg, 'text.meter-title')
Expand All @@ -237,7 +238,7 @@ export class MeterTitle extends Title {
.append('text')
.classed('percent-value', true)
.merge(percentage as any)
.text((d: any) => `${d}%`)
.text((d: any) => `${numberFormatter(d, code)}%`)
.attr('x', +title.attr('x') + title.node().getComputedTextLength() + offset) // set the position to after the title
.attr('y', title.attr('y'))

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/components/essentials/tooltip-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class AxisChartsTooltip extends Tooltip {
}

items.push({
label: options.tooltip.groupLabel,
label: get(options, 'locale.translations.group') || get(options, 'tooltip.groupLabel'),
value: datum[groupMapsTo],
color: this.model.getFillColor(datum[groupMapsTo]),
class: this.model.getColorClassName({
Expand Down Expand Up @@ -113,7 +113,10 @@ export class AxisChartsTooltip extends Tooltip {
// use the primary/only range id
const rangeIdentifier = cartesianScales.getRangeIdentifier()
items.push({
label: get(options, 'tooltip.totalLabel') || 'Total',
label:
get(options, 'locale.translations.total') ||
get(options, 'tooltip.totalLabel') ||
'Total',
value: data.reduce(
(accumulator: number, datum: any) => accumulator + datum[rangeIdentifier],
0
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/components/essentials/tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { select, pointer } from 'd3'
import { format } from 'date-fns/format'
import Position, { PLACEMENTS } from '@carbon/utils-position' // position service
import { getProperty, truncateLabel } from '@/tools'
import { zoomBar as zoomBarConfigs, tooltips as tooltipConfigs } from '@/configuration'
Expand Down Expand Up @@ -182,25 +181,27 @@ export class Tooltip extends Component {
valueFormatter(value: any, label: string) {
const options = this.getOptions()
const valueFormatter = getProperty(options, 'tooltip', 'valueFormatter')
const { code, number: numberFormatter, date: dateFormatter } = getProperty(options, 'locale')

if (valueFormatter) {
return valueFormatter(value, label)
}

if (typeof value.getTime === 'function') {
return format(value, 'MMM d, yyyy')
return dateFormatter(value, code, { month: 'short', day: 'numeric', year: 'numeric' })
}

try {
// it's a correct ISO format Date string
if (typeof value === 'string' && /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(value)) {
return format(Date.parse(value), 'MMM d, yyyy')
const newDate = new Date(value)
return dateFormatter(newDate, code, { month: 'short', day: 'numeric', year: 'numeric' })
}
} catch (e) {
// not a valid ISO format string
}

return value.toLocaleString()
return numberFormatter(value, code)
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/components/graphs/alluvial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export class Alluvial extends Component {
this.services.domUtils.generateElementIDString(`alluvial-node-title-${d.index}`)
)

const { code, number: numberFormatter } = getProperty(options, 'locale')
// Node title - text
textNode
.append('text')
Expand All @@ -267,7 +268,7 @@ export class Alluvial extends Component {
// shift 13 pixels down to fit background container
.attr('dy', 13)
.text((d: any) => {
return `${d.name} (${d.value})`
return `${d.name} (${numberFormatter(d.value, code)})`
})
.attr('aria-label', (d: any) => {
return `${d.name} (${d.value})`
Expand Down Expand Up @@ -327,6 +328,7 @@ export class Alluvial extends Component {
addLineEventListener() {
const options = this.getOptions()
const self = this
const { number: numberFormatter, code } = getProperty(this.getOptions(), 'locale')

// Set delay to counter flashy behaviour
const debouncedLineHighlight = debounce((link, event = 'mouseover') => {
Expand Down Expand Up @@ -379,7 +381,9 @@ export class Alluvial extends Component {
items: [
{
label: datum.target.name,
value: datum.value + (options.alluvial.units ? ` ${options.alluvial.units}` : ''),
value:
numberFormatter(datum.value, code) +
(options.alluvial.units ? ` ${options.alluvial.units}` : ''),
Copy link
Member

Choose a reason for hiding this comment

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

let's not do string concatenation here, since a user might return a number through numberFormatter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should I convert the numberFormatter value to a string? since we have to append alluvial units to it.

color: strokeColor,
labelIcon: self.getRightArrowIcon()
}
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/components/graphs/boxplot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { select } from 'd3'
import { flipDomainAndRangeBasedOnOrientation, generateSVGPathString } from '@/tools'
import { flipDomainAndRangeBasedOnOrientation, generateSVGPathString, getProperty } from '@/tools'
import { boxplot as boxplotConfigs } from '@/configuration'
import { BoxplotChartModel } from '@/model/boxplot'
import { Component } from '@/components/component'
Expand Down Expand Up @@ -334,7 +334,9 @@ export class Boxplot extends Component {
hoveredElement,
items: [
{
label: options.tooltip.groupLabel,
label:
getProperty(options, 'locale', 'translations', 'group') ||
getProperty(options, 'tooltip', 'groupLabel'),
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
getProperty(options, 'tooltip', 'groupLabel'),
getProperty(options, 'tooltip', 'groupLabel') || 'Group',

value: datum[groupMapsTo],
class: self.model.getColorClassName({
classNameTypes: [ColorClassNameTypes.TOOLTIP]
Expand Down Expand Up @@ -442,7 +444,9 @@ export class Boxplot extends Component {
hoveredElement,
items: [
{
label: options.tooltip.groupLabel,
label:
getProperty(options, 'locale', 'translations', 'group') ||
getProperty(options, 'tooltip', 'groupLabel'),
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
getProperty(options, 'tooltip', 'groupLabel'),
getProperty(options, 'tooltip', 'groupLabel') || 'Group',

value: datum[groupMapsTo],
class: self.model.getColorClassName({
classNameTypes: [ColorClassNameTypes.TOOLTIP]
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/components/graphs/bullet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ export class Bullet extends Component {
hoveredElement,
items: [
{
label: options.tooltip.groupLabel || 'Group',
label:
getProperty(options, 'locale', 'translations', 'group') ||
getProperty(options, 'tooltip', 'groupLabel') ||
'Group',
value: datum[groupMapsTo],
class: self.model.getColorClassName({
classNameTypes: [ColorClassNameTypes.TOOLTIP],
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/components/graphs/circle-pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ export class CirclePack extends Component {
const options = self.model.getOptions()
totalValue = [
{
label: get(options, 'tooltip.totalLabel') || 'Total',
label:
get(options, 'locale.translations.total') ||
get(options, 'tooltip.totalLabel') ||
'Total',
value: datum.value,
bold: true
}
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/components/graphs/gauge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class Gauge extends Component {
const fontSize = valueFontSize(radius)
// Add the big number
const valueNumberGroup = DOMUtils.appendOrSelect(numbersGroup, 'g.gauge-value-number')

const { code, number: localeNumberFormatter } = getProperty(options, 'locale')
const numberFormatter = getProperty(options, 'gauge', 'numberFormatter')
const valueNumber = valueNumberGroup.selectAll('text.gauge-value-number').data([value])

Expand All @@ -210,7 +210,7 @@ export class Gauge extends Component {
.merge(valueNumber as any)
.style('font-size', `${fontSize}px`)
.attr('text-anchor', 'middle')
.text((d: any) => numberFormatter(d))
.text((d: any) => localeNumberFormatter(Number(numberFormatter(d)), code))

// add the percentage symbol beside the valueNumber
const { width: valueNumberWidth } = DOMUtils.getSVGElementSize(
Expand Down Expand Up @@ -245,7 +245,7 @@ export class Gauge extends Component {
const svg = this.getComponentContainer()
const options = this.getOptions()
const delta = this.getDelta()

const { code, number: localeNumberFormatter } = getProperty(options, 'locale')
if (!delta) {
const deltaGroup = svg.select('g.gauge-delta')

Expand Down Expand Up @@ -287,7 +287,9 @@ export class Gauge extends Component {
.merge(deltaNumber)
.attr('text-anchor', 'middle')
.style('font-size', `${deltaFontSize(radius)}px`)
.text((d: any) => `${numberFormatter(d)}${gaugeSymbol}`)
.text(
(d: any) => `${localeNumberFormatter(Number(numberFormatter(d)), code)}${gaugeSymbol}`
)

// Add the caret for the delta number
const { width: deltaNumberWidth } = DOMUtils.getSVGElementSize(
Expand Down
Loading
Loading