Key Prefixes
In Branchlate, key prefixes provide a powerful way to manage translations when dealing with complex file structures, especially in content-heavy projects where each translation key corresponds to a separate file. By using {keyPrefix}
in your languageFilePattern
, you can automatically generate translation keys based on file paths, maintain hierarchical structures, and handle translated filenames.
Understanding key prefixes
A key prefix is a placeholder used in your languageFilePattern
to capture parts of the file path and use them as translation keys. This is particularly useful when your project structure involves:
- Multiple files per language, each representing a translation key.
- Nested directories that correspond to hierarchical translation keys.
- Filenames that are consistent or translated across languages.
The {keyPrefix}
placeholder tells Branchlate to extract the corresponding part of the file path and use it as part of the translation key.
Using {keyPrefix}
in file patterns
Single {keyPrefix}
When you have a directory structure where each file represents a translation key, and filenames are the same across languages, you can use a single {keyPrefix}
in your languageFilePattern
.
Example
Suppose your project has the following directory structure:
content/
├── en/
│ ├── about-us.md
│ ├── contact.md
│ └── faq.md
└── fr/
├── about-us.md
├── contact.md
└── faq.md
Your branchlate.json
configuration would be:
{
"projectId": "my-project",
"organisationId": "my-organisation",
"languages": ["en", "fr"],
"languageFilePattern": "content/{languageCode}/{keyPrefix}.md"
}
In this configuration:
{keyPrefix}
represents the filename without extension (e.g.,about-us
,contact
).- Branchlate uses the
keyPrefix
as the translation key and the file content as the translation value.
How keys are constructed
about-us.md
inen
andfr
becomes key:about-us
contact.md
inen
andfr
becomes key:contact
faq.md
inen
andfr
becomes key:faq
Since the filenames are the same across languages, there's no need for keyPrefixMappings
.
Multiple {keyPrefix}
placeholders
If your files are organized in nested directories, you can use multiple {keyPrefix}
placeholders to capture the hierarchical structure.
Example
Directory structure:
content/
├── en/
│ ├── blog/
│ │ ├── post1.md
│ │ └── post2.md
│ └── pages/
│ ├── about-us.md
│ └── contact.md
└── fr/
├── blog/
│ ├── post1.md
│ └── post2.md
└── pages/
├── about-us.md
└── contact.md
Configuration:
{
"projectId": "my-project",
"organisationId": "my-organisation",
"languages": ["en", "fr"],
"languageFilePattern": "content/{languageCode}/{keyPrefix}/{keyPrefix}.md"
}
How keys are constructed
-
The first
{keyPrefix}
captures the directory (blog
,pages
). -
The second
{keyPrefix}
captures the filename without extension (post1
,about-us
). -
Keys are constructed using dot notation:
blog.post1
corresponds tocontent/en/blog/post1.md
andcontent/fr/blog/post1.md
pages.about-us
corresponds tocontent/en/pages/about-us.md
andcontent/fr/pages/about-us.md
Again, since the directory and filenames are the same across languages, there's no need for keyPrefixMappings
.
Combining key prefixes with sub-projects
You can combine key prefixes with sub-projects to further organize your translations, especially when you want to manage distinct sets of translation keys within a project.
Using key prefixes in sub-projects
Suppose you have a project where you want to separate translations into sub-projects based on content type (e.g., blog
, pages
), and within each sub-project, you have multiple files representing individual translation keys.
Example
Directory structure:
content/
├── en/
│ ├── blog/
│ │ ├── post1.md
│ │ └── post2.md