- #133
- #132
- #131
- #130
i18n implementations must account for different word-order logic across languages
I recently refreshed my self-made personal website template: Letter, and something that caught my attention was the following snippet:
{t('Hero-1', { name: t('Me-1') })}<br />{t('Hero-2', { number: '100' })} <a class="highlight" href={`https://www.webdong.dev/${Astro.currentLocale}/post/`}>{t('Hero-3')}</a> {t('Hero-4')} {t('Hero-5', {number: '800'})}{t('Hero-6')} <a class="highlight" href={getRelativeLocaleUrl(Astro.currentLocale || defaultLocale, '/work')}>{t('Hero-7')}</a> {t('Hero-8')}"Hero-1": "哈囉,我是{name}","Hero-2": "我撰寫超過 {number} 篇","Hero-3": "技術文章","Hero-4": "紀錄與傳達知識","Hero-5": "幫助 {number} 位以上開發者消除他們的疑惑。","Hero-6": "可以觀賞","Hero-7": "作品集","Hero-8": "了解我是如何解決真實世界問題的。",It’s a bit ugly and just thinking about replacing it is a headache — especially for languages with significantly different word order where it simply won’t work. So it’s better to write it like this, splitting by “concepts rather than word order or styling”:
<Fragment set:html={t('Hero-2', {articles: '100',articlesLink: `<a class="highlight" href="https://www.webdong.dev/${Astro.currentLocale}/post/">${t('Hero-ArticlesLink')}</a>`,developers: '800'})} /><Fragment set:html={t('Hero-3', {portfolioLink: `<a class="highlight" href="${getRelativeLocaleUrl(Astro.currentLocale || defaultLocale, '/work')}">${t('Hero-PortfolioLink')}</a>`})} />"Hero-2": "我撰寫超過 {articles} 篇{articlesLink}紀錄與傳達知識,幫助 {developers} 位以上開發者消除他們的疑惑。","Hero-3": "可以觀賞{portfolioLink}了解我是如何解決真實世界問題的。","Hero-ArticlesLink": "技術文章","Hero-PortfolioLink": "作品集",At the time I probably did this to keep the translation files pure, avoiding mixing in styles or markup structure, and to avoid potential XSS issues. However, most of the time writing the markup directly into the translations is easier… besides, I can compose it from the outside using parameters.
tis a homemade i18n translation utility; most i18n libraries should have a similar placeholder mechanism for handling translations. - #129
- #128
- #127
- #126
- #125
- #124
- #123
- #122
- #121
- #120
- #119
- #118
- #117
- #116
- #115
- #114
- #113
- #112
- #111
- #110
- #109
- #108
- #107
- #106
- #105
- #104
- #103
- #102
- #101
- #100
- #99
- #98
- #97
- #96
- #95
- #94
- #93
- #92
- #91
- #90
- #89
- #88
- #87
- #86
- #85
- #84
- #83
- #82
- #81
- #80
- #79
- #78
- #77
- #76
- #75
- #74
- #73
- #72
- #71
- #70
- #69
- #68
- #67
- #66
- #65
- #64
- #63
- #62
- #61
- #60
- #59
- #58
- #57
- #56
- #55
- #54
- #53
- #52
- #51
- #50
- #49
- #48
- #47
- #46
- #45
- #44
- #43
- #42
- #41
- #40
- #39
- #38
- #37
- #36
- #35
- #34
- #33
- #32
- #31
- #30
- #29
- #28
- #27
- #26
- #25
- #24
- #23
- #22
- #21
- #20
- #19
- #18
- #17
- #16
- #15
- #14
- #13
- #12
- #11
- #10
- #9
- #8
- #7
- #6
- #5
- #4
- #3
- #2
- #1