Skip to content

Commit 27037af

Browse files
committed
combine dictionaries and formats into locales
in both registry (they were already combined in the API just not under the hood) and in config (where you were required to split them out yourself previously)
1 parent 43b73e0 commit 27037af

File tree

8 files changed

+120
-96
lines changed

8 files changed

+120
-96
lines changed

src/lib/localize.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var Registry = require('../registry');
1717
* @param {object} gd: the graphDiv for context
1818
* gd._context.locale determines the language (& optional region/country)
1919
* the dictionary for each locale may either be supplied in
20-
* gd._context.dictionaries or globally via Plotly.register
20+
* gd._context.locales or globally via Plotly.register
2121
* @param {string} s: the string to translate
2222
*/
2323
module.exports = function localize(gd, s) {
@@ -35,14 +35,14 @@ module.exports = function localize(gd, s) {
3535
* fall back on the base.
3636
*/
3737
for(var i = 0; i < 2; i++) {
38-
var dicts = gd._context.dictionaries;
38+
var locales = gd._context.locales;
3939
for(var j = 0; j < 2; j++) {
40-
var dict = dicts[locale];
40+
var dict = (locales[locale] || {}).dictionary;
4141
if(dict) {
4242
var out = dict[s];
4343
if(out) return out;
4444
}
45-
dicts = Registry.localeRegistry;
45+
locales = Registry.localeRegistry;
4646
}
4747

4848
var baseLocale = locale.split('-')[0];

src/plot_api/plot_config.js

+79-50
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ module.exports = {
2121
// no interactivity, for export or image generation
2222
staticPlot: false,
2323

24-
// we can edit titles, move annotations, etc - sets all pieces of `edits`
25-
// unless a separate `edits` config item overrides individual parts
24+
/*
25+
* we can edit titles, move annotations, etc - sets all pieces of `edits`
26+
* unless a separate `edits` config item overrides individual parts
27+
*/
2628
editable: false,
2729
edits: {
28-
// annotationPosition: the main anchor of the annotation, which is the
29-
// text (if no arrow) or the arrow (which drags the whole thing leaving
30-
// the arrow length & direction unchanged)
30+
/*
31+
* annotationPosition: the main anchor of the annotation, which is the
32+
* text (if no arrow) or the arrow (which drags the whole thing leaving
33+
* the arrow length & direction unchanged)
34+
*/
3135
annotationPosition: false,
3236
// just for annotations with arrows, change the length and direction of the arrow
3337
annotationTail: false,
@@ -43,8 +47,10 @@ module.exports = {
4347
titleText: false
4448
},
4549

46-
// DO autosize once regardless of layout.autosize
47-
// (use default width or height values otherwise)
50+
/*
51+
* DO autosize once regardless of layout.autosize
52+
* (use default width or height values otherwise)
53+
*/
4854
autosizable: false,
4955

5056
// set the length of the undo/redo queue
@@ -68,7 +74,10 @@ module.exports = {
6874
// enable axis pan/zoom drag handles
6975
showAxisDragHandles: true,
7076

71-
// enable direct range entry at the pan/zoom drag points (drag handles must be enabled above)
77+
/*
78+
* enable direct range entry at the pan/zoom drag points
79+
* (drag handles must be enabled above)
80+
*/
7281
showAxisRangeEntryBoxes: true,
7382

7483
// link to open this plot in plotly
@@ -86,18 +95,24 @@ module.exports = {
8695
// display the mode bar (true, false, or 'hover')
8796
displayModeBar: 'hover',
8897

89-
// remove mode bar button by name
90-
// (see ./components/modebar/buttons.js for the list of names)
98+
/*
99+
* remove mode bar button by name
100+
* (see ./components/modebar/buttons.js for the list of names)
101+
*/
91102
modeBarButtonsToRemove: [],
92103

93-
// add mode bar button using config objects
94-
// (see ./components/modebar/buttons.js for list of arguments)
104+
/*
105+
* add mode bar button using config objects
106+
* (see ./components/modebar/buttons.js for list of arguments)
107+
*/
95108
modeBarButtonsToAdd: [],
96109

97-
// fully custom mode bar buttons as nested array,
98-
// where the outer arrays represents button groups, and
99-
// the inner arrays have buttons config objects or names of default buttons
100-
// (see ./components/modebar/buttons.js for more info)
110+
/*
111+
* fully custom mode bar buttons as nested array,
112+
* where the outer arrays represents button groups, and
113+
* the inner arrays have buttons config objects or names of default buttons
114+
* (see ./components/modebar/buttons.js for more info)
115+
*/
101116
modeBarButtons: false,
102117

103118
// add the plotly logo on the end of the mode bar
@@ -106,51 +121,65 @@ module.exports = {
106121
// increase the pixel ratio for Gl plot images
107122
plotGlPixelRatio: 2,
108123

109-
// background setting function
110-
// 'transparent' sets the background `layout.paper_color`
111-
// 'opaque' blends bg color with white ensuring an opaque background
112-
// or any other custom function of gd
124+
/*
125+
* background setting function
126+
* 'transparent' sets the background `layout.paper_color`
127+
* 'opaque' blends bg color with white ensuring an opaque background
128+
* or any other custom function of gd
129+
*/
113130
setBackground: 'transparent',
114131

115132
// URL to topojson files used in geo charts
116133
topojsonURL: 'https://cdn.plot.ly/',
117134

118-
// Mapbox access token (required to plot mapbox trace types)
119-
// If using an Mapbox Atlas server, set this option to '',
120-
// so that plotly.js won't attempt to authenticate to the public Mapbox server.
135+
/*
136+
* Mapbox access token (required to plot mapbox trace types)
137+
* If using an Mapbox Atlas server, set this option to '',
138+
* so that plotly.js won't attempt to authenticate to the public Mapbox server.
139+
*/
121140
mapboxAccessToken: null,
122141

123-
// Turn all console logging on or off (errors will be thrown)
124-
// This should ONLY be set via Plotly.setPlotConfig
125-
// 0: no logs
126-
// 1: warnings and errors, but not informational messages
127-
// 2: verbose logs
142+
/*
143+
* Turn all console logging on or off (errors will be thrown)
144+
* This should ONLY be set via Plotly.setPlotConfig
145+
* 0: no logs
146+
* 1: warnings and errors, but not informational messages
147+
* 2: verbose logs
148+
*/
128149
logging: 1,
129150

130-
// Set global transform to be applied to all traces with no
131-
// specification needed
151+
/*
152+
* Set global transform to be applied to all traces with no
153+
* specification needed
154+
*/
132155
globalTransforms: [],
133156

134-
// Which localization should we use?
135-
// Should be a string like 'en' or 'en-US'.
157+
/*
158+
* Which localization should we use?
159+
* Should be a string like 'en' or 'en-US'.
160+
*/
136161
locale: 'en-US',
137162

138-
// Localization dictionaries
139-
// Dictionaries can be provided either here (specific to one chart) or globally
140-
// by registering them as modules (which contain dateFormat specs as well).
141-
// Here `dictionaries` should be an object of objects
142-
// {'da': {'Reset axes': 'Nulstil aksler', ...}, ...}
143-
// When looking for a translation we look at these dictionaries first, then
144-
// the ones registered as modules. If those fail, we strip off any
145-
// regionalization ('en-US' -> 'en') and try each again
146-
dictionaries: {},
147-
148-
// Localization specs for dates and numbers
149-
// Each localization should be an object with keys matching most of d3.locale,
150-
// see https://github.com/d3/d3-3.x-api-reference/blob/master/Localization.md
151-
// {'da': {months: [...], shortMonths: [...], ...}, ...}
152-
// Unlike d3.locale, every key is optional, we will fall back on English ('en').
153-
// Currently `grouping` and `currency` are ignored for our automatic number
154-
// formatting, but can be used in custom formats.
155-
formats: {}
163+
/*
164+
* Localization definitions
165+
* Locales can be provided either here (specific to one chart) or globally
166+
* by registering them as modules.
167+
* Should be an object of objects {locale: {dictionary: {...}, format: {...}}}
168+
* {
169+
* da: {
170+
* dictionary: {'Reset axes': 'Nulstil aksler', ...},
171+
* format: {months: [...], shortMonths: [...]}
172+
* },
173+
* ...
174+
* }
175+
* All parts are optional. When looking for translation or format fields, we
176+
* look first for an exact match in a config locale, then in a registered
177+
* module. If those fail, we strip off any regionalization ('en-US' -> 'en')
178+
* and try each (config, registry) again. The final fallback for translation
179+
* is untranslated (which is US English) and for formats is the base English
180+
* (the only consequence being the last fallback date format %x is DD/MM/YYYY
181+
* instead of MM/DD/YYYY). Currently `grouping` and `currency` are ignored
182+
* for our automatic number formatting, but can be used in custom formats.
183+
*/
184+
locales: {}
156185
};

src/plots/plots.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,14 @@ function getD3FormatObj(gd) {
604604

605605
// same as localize, look for format parts in each format spec in the chain
606606
for(var i = 0; i < 2; i++) {
607-
var formats = gd._context.formats;
607+
var locales = gd._context.locales;
608608
for(var j = 0; j < 2; j++) {
609-
var formatj = formats[locale];
609+
var formatj = (locales[locale] || {}).format;
610610
if(formatj) {
611611
includeFormat(formatj);
612612
if(formatDone) break;
613613
}
614-
formats = Registry.formatRegistry;
614+
locales = Registry.localeRegistry;
615615
}
616616

617617
var baseLocale = locale.split('-')[0];
@@ -620,7 +620,7 @@ function getD3FormatObj(gd) {
620620
}
621621

622622
// lastly pick out defaults from english (non-US, as DMY is so much more common)
623-
if(!formatDone) includeFormat(Registry.formatRegistry.en);
623+
if(!formatDone) includeFormat(Registry.localeRegistry.en.format);
624624

625625
return formatObj;
626626
}

src/registry.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ exports.layoutArrayContainers = [];
2929
exports.layoutArrayRegexes = [];
3030
exports.traceLayoutAttributes = {};
3131
exports.localeRegistry = {};
32-
exports.formatRegistry = {};
3332

3433
/**
3534
* register a module as the handler for a trace type
@@ -344,7 +343,8 @@ exports.registerLocale = function(_module) {
344343

345344
var locales = exports.localeRegistry;
346345

347-
var formats = exports.formatRegistry;
346+
var localeObj = locales[locale];
347+
if(!localeObj) locales[locale] = localeObj = {};
348348

349349
// Should we use this dict for the base locale?
350350
// In case we're overwriting a previous dict for this locale, check
@@ -353,14 +353,17 @@ exports.registerLocale = function(_module) {
353353
// baseLocale already had a dict or not.
354354
// Same logic for dateFormats
355355
if(baseLocale !== locale) {
356-
if(hasDict && locales[baseLocale] === locales[locale]) {
357-
locales[baseLocale] = newDict;
356+
var baseLocaleObj = locales[baseLocale];
357+
if(!baseLocaleObj) locales[baseLocale] = baseLocaleObj = {};
358+
359+
if(hasDict && baseLocaleObj.dictionary === localeObj.dictionary) {
360+
baseLocaleObj.dictionary = newDict;
358361
}
359-
if(hasFormat && formats[baseLocale] === formats[locale]) {
360-
formats[baseLocale] = newFormat;
362+
if(hasFormat && baseLocaleObj.format === localeObj.format) {
363+
baseLocaleObj.format = newFormat;
361364
}
362365
}
363366

364-
if(hasDict) locales[locale] = newDict;
365-
if(hasFormat) formats[locale] = newFormat;
367+
if(hasDict) localeObj.dictionary = newDict;
368+
if(hasFormat) localeObj.format = newFormat;
366369
};

test/jasmine/assets/supply_defaults.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var Plots = require('@src/plots/plots');
88
module.exports = function supplyDefaults(gd) {
99
if(!gd._context) gd._context = {};
1010
if(!gd._context.locale) gd._context.locale = 'en';
11-
if(!gd._context.dictionaries) gd._context.dictionaries = {};
12-
if(!gd._context.formats) gd._context.formats = {};
11+
if(!gd._context.locales) gd._context.locales = {};
1312

1413
Plots.supplyDefaults(gd);
1514
};

test/jasmine/tests/bar_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ function mockBarPlot(dataWithoutTraceType, layout) {
15221522
data: dataWithTraceType,
15231523
layout: layout || {},
15241524
calcdata: [],
1525-
_context: {locale: 'en', dictionaries: {}}
1525+
_context: {locale: 'en', locales: {}}
15261526
};
15271527

15281528
supplyAllDefaults(gd);

0 commit comments

Comments
 (0)