Skip to content

Instantly share code, notes, and snippets.

@simon04
Created February 4, 2020 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simon04/44fd82f9b717e411e5fd5427582545ac to your computer and use it in GitHub Desktop.
Save simon04/44fd82f9b717e411e5fd5427582545ac to your computer and use it in GitHub Desktop.
node-polyglot for angularjs
---
navbar:
homepage: 'Navigate to the homepage'
user: 'Logged in as {{user}}'
load: 'Load {{smart_count}} item |||| Load {{smart_count}} items'
import Polyglot = require('node-polyglot');
import en from './en.yaml';
import de from './de.yaml';
const phases = {en, de};
export const polyglot = new Polyglot({
interpolation: {
prefix: '{{',
suffix: '}}'
}
});
setLocale('de');
export function setLocale(locale: string) {
polyglot.locale(locale);
polyglot.replace(phases[locale]);
return polyglot;
}
<nav>
<a href="/">{{ 'navbar.homepage' | t }}</a>
<span class="user">{{ 'navbar.user' | t:'user':$username }}</a>
</nav>
import Polyglot = require('node-polyglot');
function provider(polyglot: Polyglot): ng.IFilterFunction {
return filter;
/**
* Translates the given key
* @param key the i18n string
* @param variable variable name for interpolation
* @param value variable value for interpolation
*/
function filter(key: string, variable?: number | string, value?: object) {
if (typeof variable === 'number') {
return polyglot.t(key, variable);
} else if (variable) {
return polyglot.t(key, {[variable]: value});
} else {
return polyglot.t(key);
}
}
}
provider.$inject = ['polyglot'];
export default provider;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment