mirror of
https://github.com/django/django.git
synced 2025-06-06 20:19:13 +00:00
Fixed #35278 -- Refactored and functions to use helper function with extracted common logic.
This commit is contained in:
parent
54fa8b5693
commit
c39c7133a0
@ -27,38 +27,40 @@
|
|||||||
}
|
}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
function getValueFromCatalog(msgid, index) {
|
||||||
|
const value = django.catalog[msgid];
|
||||||
|
|
||||||
|
// if value in catalog is a string, return it
|
||||||
|
if (typeof value === 'string' && value) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if value in catalog is an array and index is present, return the array index if exists
|
||||||
|
if (value && value.constructor === Array && index != undefined) {
|
||||||
|
const text = value[index];
|
||||||
|
|
||||||
|
if (typeof text === 'string' && text) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
if (!django.jsi18n_initialized) {
|
if (!django.jsi18n_initialized) {
|
||||||
django.gettext = function(msgid) {
|
django.gettext = function(msgid) {
|
||||||
const value = django.catalog[msgid];
|
const value = getValueFromCatalog(msgid, 0);
|
||||||
if (typeof value === 'undefined') {
|
|
||||||
return msgid;
|
|
||||||
} else {
|
|
||||||
if (typeof value === 'string') {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
const text = value[0];
|
return value !== undefined ? value : msgid;
|
||||||
|
|
||||||
return typeof text === 'string' ? text : msgid;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
django.ngettext = function(singular, plural, count) {
|
django.ngettext = function(singular, plural, count) {
|
||||||
const value = django.catalog[singular];
|
const value = getValueFromCatalog(singular, django.pluralidx(count));
|
||||||
if (typeof value === 'undefined') {
|
|
||||||
return (count == 1) ? singular : plural;
|
|
||||||
} else {
|
|
||||||
if (value.constructor === Array) {
|
|
||||||
const text = value[django.pluralidx(count)];
|
|
||||||
|
|
||||||
if (typeof text === 'undefined') {
|
|
||||||
return (count == 1) ? singular : plural;
|
|
||||||
} else {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (value !== undefined) {
|
||||||
return value;
|
return value;
|
||||||
|
} else {
|
||||||
|
return (count == 1) ? singular : plural;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user