JSON Schema for Localization: Ensuring Translation Quality and Consistency
Learn how to use JSON Schema to validate localization files, ensure consistency across languages, and maintain translation quality standards
JSON Translate TeamJuly 10, 20257 min read
#json schema#validation#quality assurance#localization standards
JSON Schema for Localization
JSON Schema provides a powerful way to validate localization files, ensuring consistency, completeness, and quality across all language variants.
Why Use JSON Schema for Localization?
1. Consistency Enforcement
- Ensure all language files have the same structure
- Require mandatory translations
- Validate data types and formats
2. Quality Assurance
- Catch missing translations early
- Validate parameter usage in messages
- Ensure proper formatting for dates and numbers
3. Developer Experience
- Provide autocomplete and IntelliSense support
- Enable early error detection
- Document translation requirements
Basic JSON Schema Structure
Simple Object Validation
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"welcome": {
"type": "object",
"properties": {
"greeting": { "type": "string" },
"subtitle": { "type": "string" }
},
"required": ["greeting", "subtitle"]
}
},
"required": ["welcome"]
}Parameterized Messages
{
"type": "object",
"properties": {
"messages": {
"type": "object",
"properties": {
"welcome_user": {
"type": "string",
"pattern": ".*{{name}}.*"
},
"items_count": {
"type": "string",
"pattern": ".*{{count}}.*"
}
}
}
}
}Advanced Schema Features
1. Enum Validation
{
"navigation": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["loading", "success", "error"]
}
}
}
}2. Array Validation
{
"weekdays": {
"type": "array",
"items": { "type": "string" },
"minItems": 7,
"maxItems": 7
}
}3. Conditional Validation
{
"allOf": [
{
"if": {
"properties": { "currency": { "const": "EUR" } }
},
"then": {
"properties": { "locale": { "pattern": "^[a-z]{2}-[A-Z]{2}$" } }
}
}
]
}Schema-Driven Validation
1. Automated Validation Pipeline
Integrate schema validation into your build process:
// validation-script.js
const fs = require('fs');
const path = require('path');
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = JSON.parse(fs.readFileSync('schema.json', 'utf8'));
function validateLanguageFile(filePath) {
const content = JSON.parse(fs.readFileSync(filePath, 'utf8'));
const valid = ajv.validate(schema, content);
if (!valid) {
console.error(`Validation failed for ${filePath}:`);
ajv.errors.forEach(error => console.error(error));
process.exit(1);
}
}
// Validate all language files
const localesDir = './locales';
fs.readdirSync(localesDir).forEach(file => {
if (file.endsWith('.json')) {
validateLanguageFile(path.join(localesDir, file));
}
});2. Editor Integration
Configure your IDE for schema validation:
// .vscode/settings.json
{
"json.schemas": [
{
"fileMatch": ["locales/*.json"],
"url": "./schemas/localization-schema.json"
}
]
}Best Practices
1. Schema Organization
- Keep schemas in a dedicated directory
- Use descriptive file names (e.g.,
messages-schema.json) - Version your schemas alongside your code
2. Schema Documentation
- Add descriptions to all schema properties
- Include examples in schema comments
- Document validation rules clearly
3. Performance Considerations
- Cache compiled schemas in production
- Validate only when files change in development
- Use incremental validation for large files
Real-World Examples
1. E-commerce Application Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"product": {
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"description": { "type": "string", "minLength": 10 },
"price": {
"type": "object",
"properties": {
"amount": { "type": "number", "minimum": 0 },
"currency": { "type": "string", "enum": ["USD", "EUR", "GBP"] }
},
"required": ["amount", "currency"]
}
},
"required": ["name", "description", "price"]
},
"cart": {
"type": "object",
"properties": {
"add_to_cart": { "type": "string" },
"remove_from_cart": { "type": "string" },
"empty_cart": { "type": "string" }
}
}
},
"required": ["product", "cart"]
}2. Dashboard Application Schema
{
"dashboard": {
"type": "object",
"properties": {
"widgets": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": { "type": "string" },
"description": { "type": "string" },
"data_source": { "type": "string" }
},
"required": ["title", "data_source"]
}
},
"navigation": {
"type": "object",
"properties": {
"menu": {
"type": "object",
"patternProperties": {
"^[a-z_]+$": { "type": "string" }
}
}
}
}
}
}
}Integration with Translation Tools
1. Continuous Validation
- Validate translations on every commit
- Block deployments with invalid translations
- Generate reports on translation completeness
2. Automated Translation
- Use schema to guide translation extraction
- Validate translations before adding to codebase
- Generate placeholder files for new languages
Benefits of Schema-Driven Localization
1. Reduced Errors
- Catch missing translations early
- Prevent syntax errors in production
- Ensure consistent parameter usage
2. Improved Developer Experience
- Better IDE support and autocomplete
- Self-documenting translation requirements
- Easier onboarding for new developers
3. Enhanced Quality
- Consistent translation quality across languages
- Automated quality checks
- Reduced manual testing effort
Implementing JSON Schema for your localization files ensures consistency, catches errors early, and improves the overall quality of your multilingual application.
Ready to Translate Your JSON Files?
Put your knowledge into practice with our powerful JSON translation tool.
Start Translating Now