Capricorn 76
Translator example
Create .e76language files (optionally using the SDK) for each supported langage including
the text labels and their unique section/key identifiers.
The identifiers must remain the same in each language file,
and it is these identifiers that your programs refer to.
Making the text labels free to change with each language.

---------------------------
---------------------------
-- Translator example
-- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd.
--
-- Create .e76language files for each supported langage including
-- the text labels and their unique section/key identifiers.
-- The identifiers must remain the same in each language file,
-- and it is these identifiers that your programs refer to.
-- Making the text labels free to change with each language.
--
-- NOTE:
-- The ./Demos/runDemo.e76script is a utility script used to run all the examples.
-- It loads the world, calls the OnWorldLoad(), OnWorldUnload() functions, and runs the graphics engine loop, waiting for the user to press escape.
---------------------------
---------------------------

local subGui;
local originalTranslator;

---------------------------
-- runDemo.e76script calls this function when the world is loaded
---------------------------
function OnWorldLoad(worldName)
    --print('OnWorldLoad');

    ---------------------------
    -- The SDK world editor creates a worldIds.e76script when the world is saved. It contains all the entity ids.
    -- We load the IDs so that we can refer to the world entities using friendly names.
    ---------------------------
    IApp:loadScript(IWorld:getNameLong() .. '/worldIds');
    IWorld:setActiveCameraId(idCamera_cam1);

    ---------------------------
    -- Get the original language file, so we can reload it later
    ---------------------------
    originalTranslator = IApp:getTranslatorFilename();

    ---------------------------
    -- Load the starting language file
    -- Load all language files in the ScriptENGINE filesystem that are named 'exampleEnglish'.
    -- Your program's tranlations can be distributed over many files, in different locations,
    -- and all will be loaded by this function.
    ---------------------------
    IApp:loadTranslations('exampleEnglish');

    ---------------------------
    -- Load the GUI
    ---------------------------
    OnWorldLoadGui();

    ---------------------------
    -- Set an animation for agent
    ---------------------------
    IGraphics:setEntityAnimationEx(idAgent_Mech, 0, 14, true, 10);

    ---------------------------
    -- Subscribe to gui events.
    -- The graphics engine will call our callback function
    -- whenever a gui event occurs.
    ---------------------------
    subGui = IEvents:subscribeGui('OnGui');

end

---------------------------
-- Load example's specific GUI
---------------------------
function OnWorldLoadGui()
    ---------------------------
    -- Check that our gui is already loaded, by testing to see it our main tab exists.
    -- If so, then remove it, and reload the GUI using the current language file.
    -- The translations will automatically replace the GUI control labels, as they're loaded in.
    ---------------------------
    if (IGraphics:controlExists('mainTab')) then

        ---------------------------
        -- Remove our current GUI
        ---------------------------
        IGraphics:removeControl('mainTab'); -- Also removes all the control's children
    end

    ---------------------------
    -- Reload GUI using current language file.
    -- The #section#key identifiers in the GUI file will be auto-translated as loading takes place.
    -- (these identifiers are typically used for GUI labels)
    ---------------------------
    IGraphics:loadGui('help');
end

---------------------------
-- runDemo.e76script calls this function when the world is loaded
---------------------------
function OnWorldUnload(worldName)
    --print('OnWorldUnload');

    ---------------------------
    -- Unsubscribe to events.
    -- We will no longer be notified of gui events.
    ---------------------------
    IEvents:unsubscribe(subGui);

    ---------------------------
    -- Change back to the original language file
    ---------------------------
    IApp:loadTranslations(originalTranslator);
end


---------------------------
-- Gui event handler.
-- The graphics engine will call this function
-- whenever a gui event occurs.
-- We can test the event type and the gui control identifier
-- to determine what event occurred and act accordingly.
---------------------------
function OnGui(callerId, eventType, userData)

    ---------------------------
    -- If the combo box was changed, then load the relevant language file
    ---------------------------
    if (IGraphics:comboChanged('combo', callerId, eventType)) then
        local selection = IGraphics:getComboboxSelection('combo');

        -- Italian
        if      (selection == 1) then
            IApp:loadTranslations('exampleItalian');

        -- German
        elseif  (selection == 2) then
            IApp:loadTranslations('exampleGerman');

        -- French
        elseif  (selection == 3) then
            IApp:loadTranslations('exampleFrench');

        -- English
        else
            IApp:loadTranslations('exampleEnglish');
        end

        -- reload the gui
        OnWorldLoadGui();

        -- Update the combo selection
        -- But stop subscribing to events for a moment, so we don't fall into a recursive loop
        IEvents:unsubscribe(subGui);
        IGraphics:setComboboxSelection('combo', selection);
        IGraphics:setControlFocus('combo');
        subGui = IEvents:subscribeGui('OnGui');
    end
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)