question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Base language strings as translation keys

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x] support request => check the FAQ and search github for a similar issue before submitting
[x] feature request

I’m not really sure which category this issue belongs to.

I would like to use base language strings as message IDs in my templates as opposed to made up IDs:

{{ 'Log in' | translate }} vs. {{ 'HOME_LOGIN_BUTTON' | translate }}

It does not seem like it’s supported to use real strings with params though. Angular complains about template parse error (And rightly so) when doing this:

{{ 'Hello {{ name }}' | translate: { name: 'Kim' } }}

Would it maybe make sense to change {{PARAM}} to {PARAM} or something completely else, to not trip up the angular template parser?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
biesbjergcommented, Dec 6, 2016

If you want, you can close this. It’s possible to use real strings as base language (with working interpolation) using custom TranslateParser and MissingTranslationHandler, although it’s not trivial to do for beginners.

app/app.module.ts

import { TranslateModule, TranslateLoader } from 'ng2-translate';
import { TRANSLATE_PROVIDERS, translateLoaderFactory } from '../i18n';

@NgModule({
	declarations: [
		AppComponent
	],
	imports: [
		IonicModule.forRoot(AppComponent),
		SharedModule,
		TranslateModule.forRoot({
			provide: TranslateLoader,
			useFactory: translateLoaderFactory,
			deps: [Http]
		})
	],
	bootstrap: [
		IonicApp
	],
	entryComponents: [
		AppComponent
	],
	providers: [
		...TRANSLATE_PROVIDERS
	]
})
export class AppModule {
}

i18n/index.ts

import { Http } from '@angular/http';

import {
	TranslateParser,
	DefaultTranslateParser,
	MissingTranslationHandler,
	MissingTranslationHandlerParams } from 'ng2-translate';

import { TranslatePoLoader } from '@biesbjerg/ng2-translate-po-loader';

export class InterpolatedTranslateParser extends DefaultTranslateParser {
	public templateMatcher: RegExp = /{\s?([^{}\s]*)\s?}/g;
}
export class InterpolatedMissingTranslationHandler implements MissingTranslationHandler {
	public parser: TranslateParser = translateParserFactory();
	public handle(params: MissingTranslationHandlerParams) {
		// return params.translateService.parser.interpolate(params.key, params.interpolateParams);
		// Workaround until this PR is merged: https://github.com/ocombe/ng2-translate/pull/348
		return this.parser.interpolate(params.key, params.interpolateParams);
	}
}

export function translateParserFactory() {
	return new InterpolatedTranslateParser();
}
export function translateLoaderFactory(http: Http) {
	return new TranslatePoLoader(http, 'assets/i18n');
}

export const TRANSLATE_PROVIDERS = [
	{ provide: TranslateParser, useFactory: translateParserFactory },
	{ provide: MissingTranslationHandler, useClass: InterpolatedMissingTranslationHandler }
];

2reactions
biesbjergcommented, Dec 1, 2016

A little context:

I usually work with po-files (Gettext), where msgid is the full untranslated string in the base language.

We use a translation service and the translators have never seen our app and wouldn’t know what any of my made up keys means, making it impossible to do a proper translation.

Reasons (some subjective?) for using ‘real strings’ as translation keys in a project:

  • Code is easier to read
  • If you want to change the English text, you just change the english translation.
  • Text in your code is very close, if not identical, to what is shown in your app
  • Possible for an external translators to make sense of the strings and translate it
Read more comments on GitHub >

github_iconTop Results From Across the Web

Keys (Strings) - Phrase
Keys are used to identify translatable strings within software code. This allows the key to be referenced once instead of each time the...
Read more >
Everything You Need to Know About Translation Keys - OneSky
A translation key indicates where a string appears on the user interface (UI) and has a corresponding value for each target language.
Read more >
Translation Strings | Directus Docs
Translation Strings are multilingual key-value pairs that you can use throughout the app. They enable you to translate things like dropdown ...
Read more >
Translation keys: naming conventions and organizing - Lokalise
This article presents some gudelines and general suggestions to properly naming and organizing your translation keys.
Read more >
Translation Keys - BigCommerce Dev Center
Translation keys exist in JSON files and are invoked based on the user's browser language. With a Stencil theme, you can define multiple...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found