Translation Cache: Source Text Alone Is Not a Safe Key
“Save” may look the same across two projects, but its translation, capitalization, and context can differ. Using the original text’s hash as the unique cache ke
Translation caches save money and time, but a cache key built only from the source text can return the wrong translation. The same word, such as "Save", can need different translations depending on the project, the target language, or the terminology in use. Using the source text's SHA-256 hash as the unique cache key also makes it easy to reuse one project's translation in another project, or even across tenants.
This guide is for developers and localization teams who cache translations. It explains which factors belong in a cache key, how to serialize them reliably, and what to check before you reuse a stored result.
Note: The examples here use synthetic samples and were checked in an isolated environment. They do not guarantee results for every real-world file or product. Keep the original copies of important files, and verify results against your own boundaries and acceptance criteria before processing them.
Can you reuse a translation by its source text alone? The short answer
No. Build the key from every factor that changes the translation decision, and check each cached entry's status before you deliver it.
| Your situation | What to do | Why |
|---|---|---|
| Reusing a translation across two projects | Include project context and the terminology version in the key | "Save" can translate differently depending on context |
| Changing the target language | Let the key change with the target language | The translation target has changed |
| Working across tenants | Include the tenant ID, and check permissions at query time | A key isolates namespaces but does not replace permission checks |
| Getting a cache hit | Check status, creation time, source task and quality flags before delivery | Entries may come from failed, canceled or unreviewed tasks |
| Normalizing text to raise hit rates | Define the rules and test them with counterexamples | Normalization can merge texts that are not identical |
Build the cache key around one translation decision
Cache utilization is not just about hit rate. Before you reuse a result, define the conditions under which identical output is acceptable.
What the key must include
A cache key should cover at least:
- Source language
- Target language
- Terminology version
- Model or translation strategy version
- Scope of use
When context influences the translation, add contextual identifiers too.
Model names alone do not guarantee stable behavior. The server-side implementation may change even when the name stays the same. For reproducible results, record your strategy version together with the actual task information, rather than assuming a single name locks down all behavior.
The example below shows local cache key composition. It does not call any translation interface, and it does not describe the internal cache implementation of OmniDoc (Product Entry).
import hashlib
import json
def cache_key(tenant, text, source_lang, target_lang, glossary, policy):
record = {"tenant": tenant, "text": text, "source": source_lang,
"target": target_lang, "glossary": glossary, "policy": policy}
raw = json.dumps(record, ensure_ascii=False, sort_keys=True,
separators=(",", ":")).encode("utf-8")
return hashlib.sha256(raw).hexdigest()
args = ("demo-a", "保存", "zh", "en", "terms-v1", "ui-v1")
first = cache_key(*args)
assert first == cache_key(*args)
assert first != cache_key("demo-b", *args[1:])
assert first != cache_key("demo-a", "保存", "zh", "ja", "terms-v1", "ui-v1")
assert first != cache_key("demo-a", "保存", "zh", "en", "terms-v2", "ui-v1")
assert len(first) == 64
Use structured serialization to avoid ambiguous keys
Simple concatenation such as text + language + version makes field boundaries unclear, so different records can produce the same string.
Structured serialization fixes this:
- Field names make each value's role explicit.
- A fixed key order and delimiter ensure the same record always produces the same byte sequence.
This example only demonstrates a fixed protocol. If you change the serialization rules later, upgrade the cache namespace at the same time.
Check availability after a cache hit
A hit on the key does not mean the result can be delivered directly. Cache entries may come from a failed, canceled, or unreviewed task.
Each entry should record:
- Status
- Creation time
- Source task
- Quality flags, when needed
Cached results from an older terminology revision can stay for historical reference. Under the new policy, though, they should no longer count as current results.
The table below shows which changes produce a different key in this example.
| Change | Different key? | Reason |
|---|---|---|
| Tenant differs | Yes | Avoids confusion about reuse scope |
| Target language differs | Yes | The translation target has changed |
| Terminology version differs | Yes | The terminology decision has changed |
| Input and policy are both identical | No | Allows querying existing results |
A SHA-256 hash is not encryption or access control
Hashing the source text with SHA-256 does not mean the cached content is desensitized. The hash only identifies the key. Common short phrases can be enumerated, so the cached content still needs access permissions and retention policies. You can compute the hash with Python hashlib.
A tenant ID in the key provides namespace isolation. It does not replace permission checks when the cache is queried.
Define normalization rules carefully
Removing spaces, punctuation, or digits across the board can raise hit rates, but it can also merge texts that are not actually identical.
Define normalization rules explicitly. Test them with counterexamples that change meaning. Do not judge rules only by the number of calls they save.
Reuse results within your delivery workflow
If you use OmniDoc as your document translation entry point, your outer task orchestration can also keep input versions and acceptance records.
Your task agreement should decide whether to reuse existing results. This article does not establish whether the platform uses the same caching strategy, and it does not guarantee that repeated submissions will avoid creating new tasks or charges.
Reference: Python hashlib
❓ FAQ
1. Can I use the SHA-256 hash of the source text as the cache key?
You can use it as part of the key, but not as the whole key. Add the language pair, terminology version, strategy version, and scope of use, because the same source text can need different translations.
2. Does a cache hit mean I can deliver the result immediately?
No. Check the entry's status, creation time, source task, and quality flags first. Entries may come from failed, canceled, or unreviewed tasks.
3. Does a SHA-256 key protect the cached content?
No. A hash identifies the key, but it does not encrypt or desensitize the content. Keep access permissions and retention policies in place.
4. Why does structured serialization matter?
Plain concatenation can make field boundaries ambiguous. A fixed field order and delimiter give the same record the same byte sequence.
📝 Summary
A translation cache is only as reliable as its key. Include every factor that changes the translation, check each entry's status before delivery, and keep access control separate from the key.
Try it now: upload a document and get it translated with the layout intact


