String localization example
This example shows how to localize static strings inside Ano script using translate{} block. Translate block contains string to be translated with its localized versions. This example can be started as below to make printout either in finnish or spanish. Any other locale or no locale at all outputs the default string:
$ LC_ALL=fi_FI.UTF-8 ./string_localize $ LC_ALL=es_ES.UTF-8 ./string_localize
Compilation
Easiest way to get this example running is to go to examples directory in package root, and run:
$ ./build.sh string_localize
build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.
Preview
; -----------------------------------------------------------------------------------
; string_localize.ano
;
; To compile:
;
; $ ./build/ano ./examples/string_localize/string_localize.ano > \
; engine/dsl_ano.h
; -----------------------------------------------------------------------------------
;
; @ANO_SCRIPT_NAME string_localize
; @ANO_SCRIPT_VERSION 0.0.1
; @ANO_SCRIPT_DESCRIPTION Simple string translation example
;
; @ANO_FLAGS_USE_PROTOS [ ]
; @ANO_FLAGS_VAR_NAME_SUBS [ ]
; @ANO_FLAGS_VAR_WARN_UNUSED [ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; This example shows how to localize static strings inside Ano script using
; translate{} block. Translate block contains string to be translated with its
; localized versions.
;
; This example can be started as:
;
; $ LC_ALL=fi_FI.UTF-8 ./string_localize
; $ LC_ALL=es_ES.UTF-8 ./string_localize
;
; to make printout either in finnish or spanish. Any other locale or no locale
; at all outputs the default string.
;
translate {
; Do not include character set name (that's the UTF-8 part after the
; period) in locale names.
;
; First string is just one block and can be translated as a whole:
;
"This is a translation example of how different languages are supported.\n" {
fi_FI:"Tämä on käännösesimerkki siitä, miten eri kieliä tuetaan.\n"
es_ES:"Este es un ejemplo de traducción de cómo se admiten diferentes idiomas.\n"
}
; Ano compiler joins adjacent string parts together if they are same
; type for speeding up printing. Plain string type contains just
; printable characters, where escaped string type has some control
; characters in it. That's why there is two translate parts for second
; print command constructed from three parts given as parameter to
; print. The first two is combined here:
;
"My favourite pets are a cat, a dog" {
fi_FI:"Lempieläimiäni ovat kissa, koira"
es_ES:"Mis animales favoritos son un gato, un perro"
}
; Last part of second print command parameter has some control
; characters (linefeed), therefore it is not combined to first two
; parts but it is handled separately:
;
" and a hedgehog.\n" {
fi_FI:" ja siili.\n"
es_ES:" y un erizo.\n"
}
}
main [exit: 0] {
; Localize and print just one plain string:
;
print "This is a translation example of how different languages are supported.\n"
; Localize three parts, two plain strings and one with control characters:
;
; First Second Third
print "My favourite pets are " . "a cat, " . "a dog" . " and a hedgehog.\n"
}
; ------------------------------------------------------------------------------ ; string_localize.w2c ; ; To compile: ; ; $ ./build/widget ./examples/string_localize/string_localize.w2c > \ ; engine/widget_defs.h ; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------ ; string_localize.m2c ; ; To compile: ; ; $ ./build/menu ./examples/string_localize/string_localize.m2c > \ ; engine/menu_defs.h ; ------------------------------------------------------------------------------
Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.