Skip to content

Commit

Permalink
js: Make lang code handling more robust in config
Browse files Browse the repository at this point in the history
Differing internal representations of two-level language
codes, e.g. pt_BR/pt_PT or zh_CN/zh_TW, would lead to
inconsistencies when combined with overrides of i18n strings
for templating.

E.g. setting data-isso-lang=pt_BR and
data-isso-postbox-text-text-PT_BR would result in an
internal representation of data-isso-postbox-text-text-pt-br
(note: lowercase) and thus user-supplied i18n strings not
being applied.

To make this handling more robust, convert everything into
lowercase and substitute underscores for dashes.

Fixes isso-comments#982
  • Loading branch information
ix5 committed Jan 19, 2024
1 parent d01d881 commit b1e8c58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions isso/js/app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@ for (var i = 0; i < js.length; i++) {
var attr = js[i].attributes[j];
if (/^data-isso-/.test(attr.name)) {
try {
config[attr.name.substring(10)] = JSON.parse(attr.value);
// Normalize underscores to dashes so that language-specific
// strings can be caught better later on,
// e.g. data-isso-postbox-text-text-PT_BR becomes
// postbox-text-text-pt-br.
// Also note that attr.name only gives lowercase strings as per
// HTML spec, e.g. data-isso-FOO-Bar becomes foo-bar, but since
// the test environment's jest-environment-jsdom seemingly does
// not follow that convention, convert to lowercase here anyway.
config[attr.name.substring(10)
.replace(/_/g, '-')
.toLowerCase()] = JSON.parse(attr.value);
} catch (ex) {
config[attr.name.substring(10)] = attr.value;
config[attr.name.substring(10)
.replace(/_/g, '-')
.toLowerCase()] = attr.value;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion isso/js/app/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ if (!plural || !translations) {
}

var translate = function(msgid) {
return config[msgid + '-text-' + lang] ||
// Need to convert the language string to lowercase because data-isso-*
// attributes are automatically cast to lowercase as per HTML spec
// https://stackoverflow.com/questions/36176474/camel-case-in-html-tag-attributes-and-jquery-doesnt-work-why
return config[msgid + '-text-' + lang.toLowerCase()] ||
translations[msgid] ||
catalogue.en[msgid] ||
"[?" + msgid + "]";
Expand Down

0 comments on commit b1e8c58

Please sign in to comment.