Skip to content

Conversation

@PouyaMohseni
Copy link
Contributor

sample

@PouyaMohseni PouyaMohseni marked this pull request as ready for review December 3, 2025 23:56
{
"wikidata_code": "{{ language.wikidata_code }}",
"autonym": "{{ language.autonym }}",
"en_label": "{{ language.en_label }}"
Copy link
Member

Choose a reason for hiding this comment

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

I just noticed that this template file has a inline script. This is quite messy and not ideal. Can we get rid of it in this PR? Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I just took a closer look at this...

If we want to keep this rendered server-side, then probably the cleanest thing to do is pass a JSON string to the context and then load it here.

Just make the languages context object into a python object (list of dictionaries), stringify it, and then assign it directly to a variable.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's also the "Django" way: see the json_script template filter: https://docs.djangoproject.com/en/5.2/ref/templates/builtins/#json-script

{
"wikidata_code": "{{ language.wikidata_code }}",
"autonym": "{{ language.autonym }}",
"en_label": "{{ language.en_label }}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I just took a closer look at this...

If we want to keep this rendered server-side, then probably the cleanest thing to do is pass a JSON string to the context and then load it here.

Just make the languages context object into a python object (list of dictionaries), stringify it, and then assign it directly to a variable.


const languages = readLanguagesFromPage();

export { languages };
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this intended to be used elsewhere? Especially since the export having any useful data depends on the languages being templated into the html...if we were populating this with an api call, I would understand exporting just because, but as it stands now, I think it's clearer to not export (if someone wants this language object later somewhere else, the fact that it isn't exported will be a good hint that it's not self-contained)

@yinanazhou
Copy link
Member

I've done the same thing here for instruments_chart_data as an example:

<script type="application/json" id="instruments-chart-data">{{ instruments_chart_data|safe }}</script>

instruments_chart_data = []
for instrument in top_instruments_by_languages:
# Get the English name if available, otherwise use the first available name
try:
english_name = instrument.instrumentname_set.filter(
language__en_label="English"
).first()
name = (
english_name.name
if english_name
else instrument.instrumentname_set.first().name
)
except:
name = f"Instrument {instrument.wikidata_id}"
instruments_chart_data.append(
{"name": name, "count": instrument.language_count}
)

@yinanazhou
Copy link
Member

Forgot to include, you can then parse data to frontend like this:

const instrumentsDataElement = document.getElementById(
'instruments-chart-data',
);
const languagesDataElement = document.getElementById(
'languages-chart-data',
);
if (!instrumentsDataElement || !languagesDataElement) {
console.error('Chart data elements not found');
return;
}


declare const languages: WikidataLanguage[];
// Helper to read JSON from the template-inserted script tag
function readLanguagesFromPage() {
Copy link
Member

Choose a reason for hiding this comment

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

This function is only called once? Does it need to be a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it would be easier to reuse it in the future, as we want to have a page to add new instruments, which would need such functionality.

Copy link
Member

@yinanazhou yinanazhou Dec 10, 2025

Choose a reason for hiding this comment

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

In that case, I would suggest to put it into a file named utils.ts and name the function getLanguages.

);

if (lang) {
nameInput.setAttribute('dir', lang.html_direction);
Copy link
Member

Choose a reason for hiding this comment

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

You are setting the text alignment below. Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On my device, text alignment was sufficient, but I wasn’t aware of the other behaviors, so I added it just to be safe. If you recommend otherwise, I remove it.

Copy link
Member

Choose a reason for hiding this comment

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

To make it more clear, I suggest you use css class instead.

Suggested change
nameInput.setAttribute('dir', lang.html_direction);
nameInput.classList.add(lang.html_direction === 'rtl' ? 'force-rtl' : 'force-ltr');

Copy link
Contributor

@dchiller dchiller left a comment

Choose a reason for hiding this comment

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

I'm good once @yinanazhou is...

lang.html_direction === 'rtl' ? 'force-rtl' : 'force-ltr',
);
nameInput.style.textAlign =
lang.html_direction === 'rtl' ? 'right' : 'left';
Copy link
Member

Choose a reason for hiding this comment

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

Is this still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it doesn't work on my side without the second part.

Copy link
Member

@yinanazhou yinanazhou Dec 12, 2025

Choose a reason for hiding this comment

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

I think it's better to make .force-rtl and .force-ltr work than doing this. Just had a closer look at them, you can try move them out of the nest they are in right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yinanazhou, Do you approve the changes?

);

if (lang) {
nameInput.classList.remove('force-rtl', 'force-ltr');
Copy link
Member

@yinanazhou yinanazhou Dec 19, 2025

Choose a reason for hiding this comment

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

This is a weird place to clean up the class. You should always clean up when the language changes. Move this outside of if and up to make it the first thing to do when the change happens. Also remember to trigger event in restoreFormData() after L307 to make sure the change event gets fired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants