diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-07-09 22:10:02 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-07-09 22:10:02 +0200 |
commit | 419882a889df4198f1af811381f1958e23211ceb (patch) | |
tree | eb611322296b785d74628234fb64eff93fda9ed2 /_plugins/i18n.rb | |
parent | ddb547694fbc649933328effd1ae08dab5eb1eea (diff) |
Diffstat (limited to '_plugins/i18n.rb')
-rw-r--r-- | _plugins/i18n.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/_plugins/i18n.rb b/_plugins/i18n.rb new file mode 100644 index 0000000..1bfb8ce --- /dev/null +++ b/_plugins/i18n.rb @@ -0,0 +1,26 @@ +class I18N < Liquid::Tag + def initialize(tag_name, text, tokens) + super + @key = text.strip() + end + + def render(context) + # see _data/i18n.yml + db = context.registers[:site].data['i18n'] + lang = context.registers[:page]['lang'] + + entry = db[@key] + return @key if entry == nil + + # first translation is the preferred translation + translation = entry.values[0] + # return key as-is if the entry is an empty map + return @key if translation == nil + # return the correct translation if it is in the entry + return entry[lang] if entry.key?(lang) + # else, return the first translation + return translation + end +end + +Liquid::Template.register_tag('i18n', I18N) |