Skip to main content

Stop Apostrophes and Quotes From Breaking Translated Strings

2026-12-01

Stop Apostrophes and Quotes From Breaking Translated Strings

The English build compiles. Then the French strings land and Android fails with error: Apostrophe not preceded by \. Or the build passes, and a Java screen shows Impossible d'ouvrir {0} with the raw placeholder still in it. Both bugs come from one character, the ASCII apostrophe, and from the fact that every layer a string passes through has a different rule for it.

If you own the path from translation files to rendered text, you can stop these bugs with three things: knowing why "apostrophe not preceded by" errors and swallowed placeholders appear after translation, knowing the rule each layer applies, and a CI check that renders every locale the way the translator wrote it.

The short answer

  1. Assume translations will add apostrophes and quotation marks your source never had, and validate every locale file.
  2. Write down each layer a string passes through (resource file, message formatter, JSON, HTML attribute) and the quote rule of each.
  3. Make the rule uniform inside a bundle. Mixed rules are where doubled or missing apostrophes come from.
  4. Store raw text in your translation system and apply escaping when you generate each file format.
  5. In CI, render every message in every locale with sample arguments and fail on leftover placeholders, doubled quotes, or parse errors.

Why the translation brings the apostrophe

English UI copy uses apostrophes mostly in contractions, and many teams write "cannot" and "do not" in product strings. French, Italian, and Catalan use the apostrophe for elision in ordinary grammar: l'application, jusqu'à, d'un fichier, dell'app, l'arxiu. A short French sentence can contain three of them. So the English file passes every check, and the problems start when the translation arrives.

An apostrophe that is harmless in the English source often arrives with the French, Italian, or Catalan translation, so escaping has to be checked on every translated file, not only on the source strings. Most tutorials show escaping on an English example such as don't, which suggests that the developer who writes the source string is the one who has to think about it. The developer usually never sees the risky string. A translator, a vendor, or a machine translation step writes it, often in a tool that shows plain text without the file format's rules.

The practitioner record matches. The Stack Overflow question Error in strings.xml file in Android has about 157,000 views for the Apostrophe not preceded by \ build error. A GitHub issue in the dreeve project, filed in October 2026, traces a 500 error on every save in the fr_FR locale to the French string jusqu'à, which ended a single-quoted HTML attribute early. Both are author reports, and both describe a character that a translation introduced.

Every layer has its own quote rule

Before fixing anything, list the layers. The rules below come from each layer's own documentation.

Android string resources

The Android string resources guide says a single quote must be written as \', or the whole string must be wrapped in double quotes, as in "This'll work". Surrounding the string with single quotes does not work. A double quote inside the text is written \". The guide also notes that Android escaping happens after the file is parsed as XML, so ' does not help: the XML parser turns it back into a bare apostrophe before Android's own rules run.

That last detail explains a common failed fix. In Apostrophe not preceded by \, with about 83,000 views, the asker tried \', ', and CDATA on a string that also contained braces. Only some of these work, and the braces hint that a second formatter was reading the same string.

<!-- Fails the build -->
<string name="open_error">Impossible d'ouvrir le fichier</string>

<!-- Works: backslash escape -->
<string name="open_error">Impossible d\'ouvrir le fichier</string>

<!-- Works: whole value in double quotes -->
<string name="open_error">"Impossible d'ouvrir le fichier"</string>

How ICU message syntax quotes text

In ICU MessageFormat the apostrophe is the quoting character. The ICU messages guide says syntax characters in text are quoted by enclosing them in pairs of ASCII apostrophes, and a pair of ASCII apostrophes always produces one apostrophe. It also notes that before ICU 4.8 an apostrophe always started quoted text, which is why older code and older answers disagree.

ICU4J exposes the choice as MessagePattern.ApostropheMode. The default, DOUBLE_OPTIONAL, lets I don't know stand as written because the apostrophe is not next to a syntax character. Under DOUBLE_REQUIRED the same text must be written I don''t know. A French translator who writes l'{appName} or d'{fileName} puts an apostrophe right before a brace, and in both modes that apostrophe starts quoted text and hides the argument. A string like jusqu'à {date} is safe under the default mode and breaks under DOUBLE_REQUIRED, so the same translation can pass in one project and fail in another.

FormatJS documents the same behavior for JavaScript: the ASCII apostrophe (U+0027) escapes syntax characters in the text portion of a message. Flutter's generated localizations follow ICU only when you ask. The Flutter internationalization guide says to set use-escaping: true in l10n.yaml, after which text between single quotes is literal and a real single quote is written as two.

How the Java formatter and Spring treat quotes

The Java MessageFormat reference is stricter than ICU's default. A single quote itself must be represented by doubled single quotes throughout the pattern, and a pair of single quotes quotes any text, including braces. So Impossible d'ouvrir {0} prints the literal text Impossible douvrir {0}: the apostrophe disappears and the argument is never formatted. The Stack Overflow questions Escaping single quotes for Java MessageFormat and MessageFormat not formatting when single quotes are included describe exactly this.

Spring adds a twist. Spring and similar wrappers skip MessageFormat parsing for messages without arguments by default, so the same doubled apostrophe renders correctly in one string and as two quotes in another unless you make the rule uniform. The Spring MessageSourceSupport reference states that alwaysUseMessageFormat defaults to false, so messages without arguments are returned as is. The asker of a Stack Overflow question about Spring resource bundles summarizes the result: translators must write '' in messages that have a placeholder and ' in messages that do not.

That two-rule setup fails during translation for a simple reason. The source string Saved has no argument and no apostrophe. The French Modifications enregistrées is also fine. Next release, someone changes the source to Saved {0} items, the message now has an argument, and the old French apostrophes in that key are reinterpreted. Translators cannot track whether a key currently has arguments, so make the rule independent of that. Set alwaysUseMessageFormat to true and double every apostrophe, or keep it false and forbid ASCII apostrophes in messages with arguments. Either works if the whole bundle follows it.

When layers stack

The hardest bugs come from strings that pass through more than one layer. One string can pass through Android resource escaping, then MessageFormat quoting, then JSON or HTML attribute encoding, so test the final rendered output in each locale instead of trusting any single layer.

Three stacking patterns show up repeatedly:

  • Resource file plus formatter. A Java or Kotlin app loads a string from strings.xml and passes it to MessageFormat. The French text needs \' for Android, and then the apostrophe that survives needs doubling for MessageFormat. Escaping for only one of them either fails the build or drops the placeholder.
  • Formatter output inside markup. In the dreeve issue, a translated ICU message was JSON-encoded and placed in a single-quoted HTML attribute with autoescaping off. JSON encoding does not escape ', so the French apostrophe closed the attribute. Each layer behaved as documented. The combination broke.
  • Build tools that rewrite messages. A FormatJS issue reports that a build step which parses and reprints messages dropped the apostrophe escape around tag-like text, so the next parse failed. Any tool that converts or flattens messages can lose escapes, and so can a generator that writes one platform's files from another's. The leaves-of-blocks issue about generating Android strings.xml from a String Catalog lists apostrophes and resource escaping as something the generator has to handle.

So a unit test of one layer proves little. The test that matters loads the real file for the real locale through the real code path and checks the string the user would see.

Pick an apostrophe policy per language

Many French, Italian, and Catalan style guides prefer the typographic apostrophe (’, U+2019) over the ASCII one in published text. That choice also removes most of the engineering risk. The ICU guide and FormatJS both define quoting in terms of the ASCII apostrophe, U+0027, and Android's rule is about the ASCII single quote too. A typographic apostrophe is ordinary text to all of them.

As a recommendation, consider a per-language policy: in languages where the typographic apostrophe is acceptable to your reviewers, ask translators and machine translation to use it, and convert ASCII apostrophes in translated UI strings as part of import. This is a synthesis from the documented rules above, not a guarantee from any vendor, and it has limits:

  • Some teams or markets prefer the ASCII apostrophe, and a reviewer may reject the change.
  • Search and matching code must treat both characters as equal if users type the ASCII one.
  • It does nothing for double quotes. Locale quotation marks such as « » in French or „ “ in German avoid the delimiter problem in the same way, but only if your style guide allows them.
  • Never convert apostrophes inside code spans, URLs, or placeholders.

Even with this policy, keep the escaping rules and checks below. Someone will paste an ASCII apostrophe eventually.

Escape at serialization, not by hand

Hand-escaped text in a translation memory spreads mistakes. If one translator writes d\'un and another writes d''un, both forms get reused in contexts where they are wrong. Keep escaping out of the stored text instead.

  1. Store unescaped text in your translation system: Impossible d'ouvrir {fileName}.
  2. Record, per key, which formatter reads it (plain, ICU, Java MessageFormat) so the exporter knows whether apostrophes need doubling.
  3. Let the exporter apply the file format's escaping when it writes strings.xml, .properties, ARB, or JSON.
  4. Give translators and AI translation the format context, so they keep placeholders intact and do not add escapes of their own.

The i18nAgent Android integration translates resource files directly, and the earlier guide on validating formatting and plurals during AI app localization covers the placeholder side of the same problem. If you still build sentences by joining fragments, fix that first. The guide to stop concatenating strings explains why, and full messages with named arguments are easier to escape correctly.

Check every locale in CI

A per-locale check catches what reviews miss. The sketch below covers two common cases: Android resources and ICU-style messages in JSON. The first loop flags Android values with an unescaped ASCII apostrophe unless the whole value is double-quoted. The second flags ICU messages where an apostrophe sits directly before a syntax character, which starts quoted text. It reports problems and does not rewrite files.

import json, re, sys
import xml.etree.ElementTree as ET
from pathlib import Path

problems = []

for path in Path("app/src/main/res").glob("values*/strings.xml"):
    for node in ET.parse(path).getroot().iter("string"):
        text = "".join(node.itertext())
        if text.startswith('"') and text.endswith('"'):
            continue
        if re.search(r"(?<!\\)'", text):
            problems.append(f"{path}: {node.get('name')}: unescaped apostrophe")

for path in Path("locales").glob("*.json"):
    for key, msg in json.load(open(path, encoding="utf-8")).items():
        if re.search(r"(?<!')'(?=[{}#|])", msg):
            problems.append(f"{path}: {key}: apostrophe before syntax character")

print("\n".join(problems))
sys.exit(1 if problems else 0)

Static patterns catch the common mistakes, but the stronger check renders messages. For each locale, format every message through the same library your app uses with sample arguments, then fail when the output still contains { followed by an argument name, contains '', or throws a parse error. Compare the number of arguments in the output to the source message. Translation platforms offer similar checks: Weblate's checks documentation describes an ICU MessageFormat check for syntax errors and placeholder mismatches, which you enable with a flag.

Run the check on every pull request that touches translation files, and on the generated files after export, because the exporter is one of the layers.

Failure handling and tradeoffs

  • A locale fails the build at release time. Do not hand-edit the generated file. Fix the source text or the exporter rule, regenerate, and rerun the check, so the next export does not undo the fix.
  • You cannot change the formatter's mode. Document the rule for that bundle in the translator brief and enforce it with the lint. A known odd rule beats a silent mix of two.
  • Typographic apostrophes break matching. Normalize both characters to one form in search and comparison code, and keep the display text as the translator wrote it.
  • Old releases read the same remote strings. If you ship strings over the air, a change in escaping can break older app versions that use a different formatter setting. Version the payload or keep the old rule for old clients.

Verify before you ship

  • Every strings.xml in every values-* folder builds without Apostrophe not preceded by errors.
  • Each bundle has one written apostrophe rule, and the lint enforces it.
  • Messages with arguments render with sample values in every locale, with no raw braces and no doubled quotes.
  • Strings embedded in JSON, HTML attributes, or JavaScript are encoded for that context, and a test renders at least one French message through that path.
  • Any tool that converts, flattens, or generates message files is covered by the same render check on its output.

What to do next

Take your French or Italian files and run the two checks above against them today. Count the unescaped apostrophes and the apostrophes that sit right before a brace. Then write the apostrophe rule for each bundle, add the render check to CI, and move escaping out of the translation text and into your exporter so the next translation round cannot break the build again.