-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_language_usage.py
More file actions
executable file
·124 lines (93 loc) · 3.82 KB
/
example_language_usage.py
File metadata and controls
executable file
·124 lines (93 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
Example: Using language configuration to load localized documentation
This script demonstrates how to use the language configuration from
project_config.yaml to load documentation in the appropriate language.
"""
import yaml
import os
from pathlib import Path
def get_documentation_language(environment=None):
"""
Get the documentation language for the current environment.
Args:
environment: Environment name (development, staging, production)
If None, reads from ENVIRONMENT env var
Returns:
tuple: (language, fallback_language)
"""
with open('project_config.yaml', 'r') as f:
config = yaml.safe_load(f)
# Determine environment
if environment is None:
environment = os.getenv('ENVIRONMENT', 'development')
doc_config = config['documentation']
env_config = doc_config['environments'].get(environment, {})
# Priority: ENV VAR > environment config > default
language = os.getenv('DOC_LANGUAGE',
env_config.get('language',
doc_config['languages']['default']))
fallback = os.getenv('DOC_FALLBACK_LANGUAGE',
env_config.get('fallback_language', 'en'))
return language, fallback
def load_localized_doc(doc_name, language=None, fallback=None):
"""
Load a documentation file in the appropriate language.
Args:
doc_name: Name of the documentation file (without extension)
language: Primary language to try (if None, gets from config)
fallback: Fallback language (if None, gets from config)
Returns:
str: Content of the documentation file
"""
if language is None or fallback is None:
lang, fb = get_documentation_language()
language = language or lang
fallback = fallback or fb
docs_dir = Path('docs')
# Try primary language
primary_path = docs_dir / f"{doc_name}.{language}.md"
if primary_path.exists():
return primary_path.read_text()
# Try fallback language
fallback_path = docs_dir / f"{doc_name}.{fallback}.md"
if fallback_path.exists():
return fallback_path.read_text()
# Try without language suffix
default_path = docs_dir / f"{doc_name}.md"
if default_path.exists():
return default_path.read_text()
raise FileNotFoundError(f"Documentation '{doc_name}' not found in any language")
def main():
"""Example usage"""
print("=" * 60)
print("Language Configuration Example")
print("=" * 60)
# Get current language configuration
language, fallback = get_documentation_language()
print(f"\nCurrent Configuration:")
print(f" Environment: {os.getenv('ENVIRONMENT', 'development')}")
print(f" Primary Language: {language}")
print(f" Fallback Language: {fallback}")
# Example: Load configuration
with open('project_config.yaml', 'r') as f:
config = yaml.safe_load(f)
print(f"\nSupported Languages:")
supported = config['documentation']['languages']['supported']
for lang in supported:
status = "✓ (current)" if lang == language else ""
print(f" - {lang} {status}")
print(f"\nEnvironment Configurations:")
environments = config['documentation']['environments']
for env_name, env_config in environments.items():
print(f" {env_name}:")
print(f" Language: {env_config['language']}")
print(f" Fallback: {env_config['fallback_language']}")
print("\n" + "=" * 60)
print("You can override using environment variables:")
print(" export ENVIRONMENT=staging")
print(" export DOC_LANGUAGE=en")
print(" export DOC_FALLBACK_LANGUAGE=es")
print("=" * 60)
if __name__ == '__main__':
main()