|
| 1 | +from pathlib import Path |
| 2 | +import json |
| 3 | + |
| 4 | +from docutils import nodes |
| 5 | +from docutils.parsers.rst import Directive |
| 6 | +import plotly.graph_objects as go |
| 7 | +from plotly.offline import plot |
| 8 | + |
| 9 | +class TranslationGraph(Directive): |
| 10 | + # Tells Sphinx that this directive can be used in the document body |
| 11 | + # and has no content |
| 12 | + has_content = False |
| 13 | + |
| 14 | + def run(self): |
| 15 | + # Read the JSON file containing translation statistics |
| 16 | + json_path = Path(__file__).parent.parent / '_static' / 'translation_stats.json' |
| 17 | + with json_path.open('r') as f: |
| 18 | + data = json.load(f) |
| 19 | + |
| 20 | + # Collect all module names -- iterates over the JSON data in 2 levels |
| 21 | + all_modules = {module for stats in data.values() for module in stats} |
| 22 | + all_modules = sorted(all_modules) |
| 23 | + |
| 24 | + # Build one trace per locale with full hover info |
| 25 | + traces = [] |
| 26 | + |
| 27 | + for locale, modules in data.items(): |
| 28 | + y_vals = [] |
| 29 | + hover_texts = [] |
| 30 | + |
| 31 | + for module in all_modules: |
| 32 | + stats = modules.get(module) |
| 33 | + y_vals.append(stats["percentage"]) |
| 34 | + |
| 35 | + hover_text = ( |
| 36 | + f"<b>{module}</b><br>" |
| 37 | + f"Translated: {stats['translated']}<br>" |
| 38 | + f"Fuzzy: {stats['fuzzy']}<br>" |
| 39 | + f"Untranslated: {stats['untranslated']}<br>" |
| 40 | + f"Total: {stats['total']}<br>" |
| 41 | + f"Completed: {stats['percentage']}%" |
| 42 | + ) |
| 43 | + hover_texts.append(hover_text) |
| 44 | + |
| 45 | + traces.append(go.Bar( |
| 46 | + name=locale, |
| 47 | + x=all_modules, |
| 48 | + y=y_vals, |
| 49 | + hovertext=hover_texts, |
| 50 | + hoverinfo="text" |
| 51 | + )) |
| 52 | + |
| 53 | + # Create figure |
| 54 | + fig = go.Figure(data=traces) |
| 55 | + fig.update_layout( |
| 56 | + barmode='group', |
| 57 | + title="Translation Coverage by Module and Locale", |
| 58 | + xaxis_title="Module", |
| 59 | + yaxis_title="Percentage Translated", |
| 60 | + height=600, |
| 61 | + margin=dict(l=40, r=40, t=40, b=40) |
| 62 | + ) |
| 63 | + |
| 64 | + div = plot(fig, output_type='div', include_plotlyjs=True) |
| 65 | + return [nodes.raw('', div, format='html')] |
| 66 | + |
| 67 | +def setup(app): |
| 68 | + app.add_directive("translation-graph", TranslationGraph) |
| 69 | + return { |
| 70 | + "version": "0.1", |
| 71 | + "parallel_read_safe": True, |
| 72 | + "parallel_write_safe": True, |
| 73 | + } |
0 commit comments